0

Is there a way to write a member/method reference to return the object itself in Kotlin? That is to say, it can simplify and replace the following lambda:

{ it }
Shreck Ye
  • 1,591
  • 2
  • 16
  • 32
  • 1
    I think you should be able to use the answer from this question https://stackoverflow.com/questions/32336436/does-kotlin-have-an-identity-function – Matt Berteaux Feb 01 '19 at 18:31

1 Answers1

2

There isn't a built in way as far as I know. Best you could do is write a function that does this once, and then refer to that when you need it, e.g.

inline fun <T> identity(t: T) = t

And then usage like so:

"foo".let(::identity) // still "foo"
zsmb13
  • 85,752
  • 11
  • 221
  • 226