public get<R>(key as K, defaultValue as R) as R:
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 MapSync<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.
| Parameter | Description |
|---|---|
key | The key to search for. |
defaultValue | The typed value returned when the key is absent. |
The stored value cast to R, or defaultValue.
import klyn.collections
config = MapSync<String, Object>()
config["theme"] = "dark"
config["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.