Wiki
Show
Ignore:
Timestamp:
03/14/10 08:28:09 (2 years ago)
Author:
Chuck.Esterbrook
Message:

Added IDictionary<of TKey, TValue>.get(key as TKey, default as TValue) as TValue

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/Cobra.Lang/ExtendDictionary.cobra

    r2246 r2318  
    1818                return newDict 
    1919 
     20        def get(key as TKey, default as TValue) as TValue 
     21            """ 
     22            Returns the value for the given key, or returns the default if the key is not found. 
     23            """ 
     24            ensure not .containsKey(key) implies result == default 
     25            value as TValue 
     26            if .tryGetValue(key, out value), return value 
     27            else, return default 
     28 
     29 
    2030    class TestIDictionaryExtensions 
    2131 
     
    3242            assert b.count == 2 
    3343            assert b['x'] == 1 and b['y'] == 2 
     44 
     45            d = {'a': 1, 'b': 2} 
     46            assert d.get('a', 0) == 1 
     47            assert d.get('c', 1) == 1