Extensions: val vs zero-arg fun


#1

If you’re creating an extension, and your choices are either a val (extension property) or zero-arg fun (extension method)…what are people’s criteria in making that choice?

I find myself relying on “gut feeling” than I like, so…if there are any rules/guidelines I should know about, I’d love to hear them.

Thanks!


#2

When it is a non-trivially computed value I don’t use properties.


#3

Coding Conventions: Functions vs Properties


#4

Thanks, I was actually looking on this page for something like this, but didn’t find it.

I was hoping for more semantic guidelines. To me, properties make sense when you’re accessing an attribute (field or computed) of an instance, while methods are more like operations that do real work. But if you have a property that requires some non-trivial amount of work to calculate…it blurs the lines.


#5

Yeah it does. I think it’s up to each programmer to decide case by case what is to much work for a property and what isn’t.

Another rule I would add to the style is that properties should have no side effects. At least the getter shouldn’t. Maybe it’s even worth to create a PR to add this rule.


#6

At least no external side effects. Property could be lazy and thus change internal state of the object.