lundi 6 avril 2020

Property references vs. lambdas for getter/setter

I need to get and set a property of another class from a method and therefore need to pass in either the property reference of lambdas for the getter and the setter:

  1. Passing in the property reference

    otherInstance::property

  2. Passing in a lambda for the getter and one for the setter:

    {otherInstance.property} // getter

    {value -> otherInstance.property = value} // setter

I like the first one, because for me the code is easier to read and shorter, but my alarm bells ring when I read about it on the official documentation, because of the term "reflection". My knowledge from Java is that reflection generally isn't a good thing. Is that also valid with Kotlin? Is it valid with this case? Is one of both ways (property reference or lambdas) more performant or more safe?





Aucun commentaire:

Enregistrer un commentaire