Why I'm Excited about D
Idan Arye via Digitalmars-d
digitalmars-d at puremagic.com
Sun Apr 12 14:11:41 PDT 2015
On Sunday, 12 April 2015 at 18:42:01 UTC, Andrei Alexandrescu
wrote:
> On 4/12/15 10:43 AM, Dicebot wrote:
>> It is much better in languages that do support dedicated
>> properties
>> because language rules can be used to express the same notion
>> without
>> resorting to any conventions.
>
> Wouldn't e.g. C# allow arbitrary code in getters and setters?
> And if so, wouldn't C# code need to resort to conventions to
> avoid surprising behavior? -- Andrei
I'd be surprised if there is any language that prevents you from
causing side-effects in getters(beside functional languages that
prevent you from causing side-effects anywhere) or
non-setting-related side-effects in setters. These kind of
limitations prevent many useful patterns, like
calculating-and-caching a value the first time a getter is
called, or updating a visual component on the screen when it's
setters are called.
I don't think
relying-purely-on-conventions-with-no-language-support and
the-language-strictly-forbids-stuff are the only choices. With a
grain (syntactic) salt a language can promote the right way by
turning the wrong way into code smell.
If you see a getter function declared as getter(be it with C#'s
getter declaration syntax or with D's
doesn't-actually-do-anything @property attribute or whatever used
in that language) that causes side-effects other than the ones
pragmatically needed for getting the value, you can immediately
see that something is wrong. But if you look at such a function
at the call site you can't guess that it's doing that stuff.
In C#, if you can't rely on other developers to only use the
getter-setter syntax for actual getters and setters, then that is
probably the least of your worries...
In D, you don't need to not rely on other developers to do that,
because EVERY method without arguments and EVERY function with a
single argument are getters, and there is nothing you can do
about it(unless you use the seems-to-be-abandoned -property flag).
BTW - the exact definition of getters and setters may vary(can
querying a webservice be considered a getter?), but under no sane
definition is writeln a getter!
More information about the Digitalmars-d
mailing list