Proposal: side-effect free getters It's a pretty ...
# language-proposals
j
Proposal: side-effect free getters It's a pretty well-known problem that if you split a project into multiple modules, you lose some smart casting because the compiler cannot assume that a getter in another module will always return the same result. I'd like to propose adding an annotation that indicates that the library author intends for a getter to remain side-effect free so that clients can write code more naturally. Thoughts?
👍 4
r
Could you elaborate on this? I was under the impression that a custom getter always blocked smart casting, even in the same module. Also, the getter doesn't need to be side effect free for it to return different values each time (unless I misunderstand what you mean by side effect).
j
I mean it returns the same value every time unless you call something else on the object (e.g. a setter)
This would be an annotation on the property.
It would be useful for custom getters too!
r
Ah, I misread and thought you were talking about custom getters. Sorry about that.
j
Not at all! Useful clarification.
r
I think this would be an excellent feature, but IMO it should be done using contracts rather than a bespoke annotation
j
But how would one attach a contract to a generated getter?
j
Not sure if that covers everything, but it's definitely a starting point!
Am curious if anyone has ideas on how one could attach a contract to a generated function (i.e. a function that doesn't have a body in the code)
e
I don't think this would be handled by a contract anyway, at least in the current design. they feel very per-call
if that were how it works, then you can just contract the getter like how you can annotate the getter now
‼️ 1