klyn.collections.Map.get
method

    public get<R>(key as K, defaultValue as R) as R:

Description

Returns the value associated with the key, or the provided default value when the key is absent.

Use this method when a missing key is expected and should not raise a `KeyException`.

The return type is the generic type `R`, inferred from `defaultValue` or provided explicitly with `get<R>(...)`. This keeps heterogeneous maps statically useful. For example a `Map<String, Object>` can return a `String`, an `Int` or a `Boolean` depending on the typed default passed by the caller. If the key exists, the stored value is cast to `R`; using the wrong expected type fails as a normal type error.

Parameters

Parameter | Description
--- | ---
`key` | The key to search for.
`defaultValue` | The typed value returned when the key is absent.

Returns

The stored value cast to `R`, or `defaultValue`.

Example

    import klyn.collections

    config as MapCollection<String, Object> = {
    "theme": "dark",
    "retryCount": 3
    }

    theme as String = config.get("theme", "light")            # R is String
    retries as Int = config.get("retryCount", 0)
    fallback as Boolean = config.get("missingFlag", false)
    explicit as String = config.get<String>("theme", "light") # Same type, explicit R.