# Implementation delegation "by" cannot access "val"s

**URL:** <https://discuss.kotlinlang.org/t/implementation-delegation-by-cannot-access-val-s/405>\
**Category:** Uncategorized\
**Created:** [November 20, 2014, 6:09pm UTC](https://discuss.kotlinlang.org/t/implementation-delegation-by-cannot-access-val-s/405 "2014-11-20T18:09:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![msgile](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msgile/32/1484_2.png) [@msgile](https://discuss.kotlinlang.org/u/msgile)\
**Post date:** [November 20, 2014, 6:09pm UTC](https://discuss.kotlinlang.org/t/implementation-delegation-by-cannot-access-val-s/405/1 "2014-11-20T18:09:06Z")

</div>

Kotlin support implementation delegation allowing you to eliminate boilerplate code.

```kotlin
class Container<K,E>(val map: Map<K,E> = TreeMap()) : Map<K,E> by map {
  //other stuff, new methods etc
}
```

 

All the methods of the **Map** interface are delegate to the val **map** (methods of **Map** can still be selectively overriden). However the following is not allowed:

```kotlin
class Container<K,E>() : Map<K,E> by map {
  val map: Map<K,E> = TreeMap()
  //other stuff, new methods etc
}
```

 

Any plans to support implementation delegation using vals (final variables)?

---

<div class="post-metadata">

**Author:** ![geevee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/geevee/32/1475_2.png) [@geevee](https://discuss.kotlinlang.org/u/geevee)\
**Post date:** [November 21, 2014, 2:57pm UTC](https://discuss.kotlinlang.org/t/implementation-delegation-by-cannot-access-val-s/405/2 "2014-11-21T14:57:44Z")

</div>

We have pretty old issue about it: [KT-2747](https://youtrack.jetbrains.com/issue/KT-2747). Now the semantic of delegation is the following: expression to delegate (after "by") is computed after calling superconstructor, but before other class initialization (everything in class body, including vals). It is saved in class state and never computed again.

We may allow delegation to some expression which would be executed on each delegated call (and support your case), but we haven’t decided it yet. Probably, we’ll need to decide it before Kotlin 1.0.
