Function references with default parameters

Hi!

I would like to pass path.toFile()::readText to a function which accepts () -> String.
The problem is that the readText() method has a parameter named charset. Then the function looks like (Charset) -> String. It works that way but I assumed that the charset can be omitted because it is a default parameter.

The two workarounds are:

  1. (as already explained) change the signature of the method that accepts the function to (Charset) -> String
  2. write it as a lambda: `{ path.toFile().readText() }

But path.toFile()::readText to () -> Stringis not possible.

Any ideas?

1 Like

This is a known issue and it’s going to be supported in a future release: https://youtrack.jetbrains.com/issue/KT-8834

4 Likes

As workaround
val readText :()->String = { path.toFile().readText() }

so you can use readText() directly, or you can make an extension property on File class then take reference of it

1 Like