Delegation Quiz

KOTLIN › Types & Classes

In class Derived(b: Base) : Base by b, who implements the members of Base?

Answer: The compiler emits forwarding methods that delegate to b

The by keyword makes the compiler generate methods that forward each interface member to the delegate object b, so Derived needs no manual implementations.

A delegate object b has print() that reads its own message. Derived overrides message but uses Base by b. What does derived.print() show?

Answer: b's own message, ignoring Derived's override

A delegate cannot see members overridden in the deriving class; b.print() uses b's own message, so Derived's override is ignored by the delegate's method.

Which operator function signature is required for a read-only (val) custom delegate?

Answer: operator fun getValue(thisRef: Any?, property: KProperty<*>): T

A val delegate needs operator fun getValue(thisRef, property: KProperty<*>) returning the property type; the property parameter is a KProperty reflection object.

Which standard delegate's handler runs BEFORE assignment and can reject the new value?

Answer: Delegates.vetoable

vetoable invokes its handler before applying the change and returning false vetoes it; observable fires after assignment and cannot reject it.

What is the default thread-safety mode of by lazy { ... }?

Answer: LazyThreadSafetyMode.SYNCHRONIZED (the default mode)

Without an argument, lazy uses SYNCHRONIZED, locking so only one thread initializes the value; PUBLICATION and NONE must be requested explicitly.

For class User(val map: MutableMap<String, Any?>) { var age: Int by map }, what happens on user.age = 30?

Answer: map["age"] is set to 30 via the map's setValue

MutableMap provides a setValue delegate, so assigning to a map-backed var writes the value under the property name key (map["age"] = 30).

When delegating a property with by, what does the compiler generate behind the scenes?

Answer: A hidden prop$delegate field the accessors route through

The compiler stores the delegate in a hidden prop$delegate field and routes the property's get/set through the delegate's getValue/setValue, passing this and the KProperty reference.

What is the role of an operator fun provideDelegate(thisRef, property) on a delegate?

Answer: It runs once when the delegate is bound, before any getValue call

provideDelegate is invoked once at delegate-creation (binding) time, so you can run validation or return a different delegate instance before any getValue/setValue access happens.

To write a custom var delegate without hand-writing operator functions, which standard library interface should you implement?

Answer: ReadWriteProperty<T, V>, which also extends ReadOnlyProperty

ReadWriteProperty supplies both getValue and setValue (and extends ReadOnlyProperty), so implementing it gives you a working var delegate; ReadOnlyProperty alone only supports val.

When does the handler passed to Delegates.observable run, and what can it do?

Answer: After assignment, for notification only; can't reject it

observable fires its callback with old and new values after the assignment is applied; unlike vetoable it has no return value and cannot block the change.

What happens with var counter: Int by lazy { 0 }?

Answer: Compile error: lazy returns Lazy<T>, which only has getValue

Lazy<T> exposes only a getValue operator, so it cannot back a var; the compiler rejects using lazy with a mutable property.

Under concurrent first access, how does LazyThreadSafetyMode.PUBLICATION behave?

Answer: Several threads may run it, but the first completed value is cached

PUBLICATION allows several threads to race the initializer without a lock; the first value to be set atomically wins and all callers then see that single cached value.

Inside a custom getValue implementation, how do you read the delegated property's declared name?

Answer: property.name; the KProperty parameter exposes it

The KProperty<*> argument passed to getValue/setValue exposes the property's name (and other metadata), which is exactly how map-backed delegates look up the key.

What problem does Delegates.notNull() solve?

Answer: A non-null var with deferred init that throws if read too early

notNull provides a non-null var whose initialization is deferred; reading it before a value is set throws, which is useful when the value can't be supplied at construction time.

Back to Delegation