Title
l

louiscad

03/28/2017, 12:21 PM
Anyone knows how
hashCode()
is implemented by the Kotlin compiler in a
data class
having a `fun`ction property constructor parameter? e.g.:
data class Option(val id: Int, val desc: () -> String)
m

mg6maciej

03/28/2017, 12:23 PM
It calls the function :trollface:
l

louiscad

03/28/2017, 12:24 PM
Not possible, this would be huge flaw as this is not documented, and this could have side-effects šŸ˜›
m

mg6maciej

03/28/2017, 12:25 PM
To be honest I don't know, but I'd guess it is the default hashCode, i.e. takes address of the function object.
k

kirillrakhman

03/28/2017, 12:50 PM
@louiscad why would it be a flaw? What's the difference to
data class Foo(val id: Int, val desc: Description)
where
Description
doesn't implement
hashCode()
?
m

mg6maciej

03/28/2017, 12:57 PM
That was a joke on my side.
desc.invoke().hashCode()
l

louiscad

03/28/2017, 12:58 PM
@kirillrakhman I said why it'd be a flaw to invoke the property function in
hashCode()
(and
equals(other: Any)
).
Any
already implements
hashCode()
, but it can be less efficient if you stick to the default implementation
k

kirillrakhman

03/28/2017, 12:59 PM
Ok I understand. Of course, it doesn't invoke the function.