# Function reference and function visibility

**URL:** <https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229>\
**Category:** Uncategorized\
**Created:** [April 3, 2012, 11:07am UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229 "2012-04-03T11:07:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cy6erGn0m](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cy6ergn0m/32/1439_2.png) [@cy6erGn0m](https://discuss.kotlinlang.org/u/cy6erGn0m)\
**Post date:** [April 3, 2012, 11:07am UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229/1 "2012-04-03T11:07:08Z")

</div>

I tried to use function references but again got problem. I have the code like this:

```kotlin
trait A {
    val method : (() -> Unit )?
    val test : Integer
}

class AImpl : A {
  override val method : (() -> Unit )? = {
  println(“test2”)
  }
  override val test : Integer = Integer(777)
}
fun test(a : A) {
  val method = a.method
  if (method != null) {
  method()
  }
}
public fun main(args : Array<String>) {
  println(AImpl().test)
  test(AImpl())
}
```

But it fails at runtime:

```kotlin
Exception in thread “main” java.lang.IllegalAccessError: AImpl.getMethod()Ljet/Function0;
```

but prints 777 successfully

But if I modify AImpl method and add “public”

```kotlin
    public override val method : (() -> Unit )? = {
  println(“test2”)
  }
```

then everything will OK so it looks like method implementation is class private?

Is there something I missing?

---

<div class="post-metadata">

**Author:** ![svtk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svtk/32/1499_2.png) [@svtk](https://discuss.kotlinlang.org/u/svtk)\
**Post date:** [April 3, 2012, 11:41am UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229/2 "2012-04-03T11:41:08Z")

</div>

Default visibility is 'internal', so your example should work correctly. But visibility check is in progress now, so it doesn't tested properly yet.&nbsp;&nbsp;Mind to file an issue about this to [http://youtrack.jetbrains.com/issues/KT](http://youtrack.jetbrains.com/issues/KT)? Thanks

---

<div class="post-metadata">

**Author:** ![cy6erGn0m](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cy6ergn0m/32/1439_2.png) [@cy6erGn0m](https://discuss.kotlinlang.org/u/cy6erGn0m)\
**Post date:** [April 3, 2012, 12:25pm UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229/3 "2012-04-03T12:25:47Z")

</div>

> svtk wrote: Default visibility is 'internal', so your example should work correctly.

Yes, it is but... the visiblity of variable is public but anonimous function used to initialize it probably not..

---

<div class="post-metadata">

**Author:** ![svtk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svtk/32/1499_2.png) [@svtk](https://discuss.kotlinlang.org/u/svtk)\
**Post date:** [April 3, 2012, 12:48pm UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229/4 "2012-04-03T12:48:33Z")

</div>

There is no visibility for anonymous functions. Only members of packages and classes have visibility, and anonymous function cannot be a member (it's not by chance called 'anonymous':) ).

---

<div class="post-metadata">

**Author:** ![cy6erGn0m](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cy6ergn0m/32/1439_2.png) [@cy6erGn0m](https://discuss.kotlinlang.org/u/cy6erGn0m)\
**Post date:** [April 3, 2012, 1:02pm UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229/5 "2012-04-03T13:02:37Z")

</div>

I mean underlying implementation generated by compiler. I guess compiler generates private method for this anonimous function (only guess) or somthing like that. So I mean visibility checked by JVM, not visibility in terms of Kotlin.

---

<div class="post-metadata">

**Author:** ![svtk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svtk/32/1499_2.png) [@svtk](https://discuss.kotlinlang.org/u/svtk)\
**Post date:** [April 3, 2012, 1:11pm UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229/6 "2012-04-03T13:11:15Z")

</div>

Ok, I understand what you mean. Thank you!

---

<div class="post-metadata">

**Author:** ![cy6erGn0m](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cy6ergn0m/32/1439_2.png) [@cy6erGn0m](https://discuss.kotlinlang.org/u/cy6erGn0m)\
**Post date:** [April 3, 2012, 1:16pm UTC](https://discuss.kotlinlang.org/t/function-reference-and-function-visibility/1229/7 "2012-04-03T13:16:10Z")

</div>

You are right. It was haste to make guesses :) I can see the generated classes for my example. I can see getMethod() of AImpl isn't public and "anonimous" function is generated as separate class...

```kotlin
% javap A
Compiled from "cg.Test.kt"
public interface A extends jet.JetObject{
    public abstract jet.Function0 getMethod();
    public abstract java.lang.Integer getTest();
}
% javap AImpl
Compiled from “cg.Test.kt”
public final class AImpl extends java.lang.Object implements jet.JetObject,A{
  final jet.Function0 method;
  final java.lang.Integer test;
    jet.Function0 getMethod();
  java.lang.Integer getTest();
  public AImpl();
}
% javap AImpl$method$1
Compiled from “cg.Test.kt”
public class AImpl$method$1 extends jet.Function0{
  public java.lang.Object invoke();
  public final void invoke();
  public AImpl$method$1();
  public static AImpl$method$1 $getInstance();
}

```
