namespace Cobra.Core extend IDictionary def clone as IDictionary # CC: as same """ Returns a new dictionary with the contents of this dictionary. Does not clone the contents themselves. This is sometimes known as a "shallow copy". """ ensure result is not this result.count == .count result.typeOf is .typeOf body type = .typeOf newDict = type(this) return newDict def get(key as TKey, default as TValue) as TValue """ Returns the value for the given key, or returns the default if the key is not found. """ ensure not .containsKey(key) implies result == default value as TValue if .tryGetValue(key, out value), return value else, return default class TestIDictionaryExtensions test a = Dictionary() b = a.clone assert b is not a assert b.count == 0 a = {'x': 1, 'y': 2} b = a.clone assert b is not a assert b.count == 2 assert b['x'] == 1 and b['y'] == 2 d = {'a': 1, 'b': 2} assert d.get('a', 0) == 1 assert d.get('c', 1) == 1