Proposal request: explicit propreties

Yigal Chripun yigal100 at gmail.com
Tue Apr 1 15:41:40 PDT 2008


Ary Borenszweig wrote:
> Currently properties can have these forms:
>
> Getter:
> -------
> Type property() {
>   // ...
> }
>
> Setter:
> -------
> void property(Type type) {
>   // ...
> }
>
> The problem is, the user can use them as functions or as properties.
> This is ok from the compiler point of view, but the code looks ugly if
> it doesn't make sense, like:
>
> writefln = 5;
>
> Further, if you want a function to be only used with property syntax,
> you can't. Why would you wan't that? Because if you have
>
> class Foo {
>
>   int property() {
>     //
>   }
>
> }
>
> and then you decide to change it to
>
> class Foo {
>
>   int property;
>
> }
>
> for some reason, code that used Foo.property() won't compile anymore.
>
> I suggest marking properties as such like this:
>
> getter Type property() {
>   // ...
> }
>
> setter void property() {
>   // ...
> }
>
> "getter" and "setter" are attributes, just like "public", "static",
> etc. The compiler only uses them to validate correct syntax usage. If
> they are applied to any other declaration that is not a function, an
> error is reported.
>
> Finally, there is another reason for wanting to mark functions as
> properties: when you do autocompletion in an IDE, and it suggests you
> a function, it can't know whether to autocomplete it as a function or
> as a property. A solution could be writing something in the ddoc of
> that function, but since there's no standard for this, each IDE will
> invent it's own.
>
> Of course, this is not backwards compatible, so it should be a D2
> feature.
>
> What do you think?
what about the other way around - instead of making properties explicit,
generalize implicit properties to any number or parameters. for example,
say your GUI lib has a function to set the size of a window:
void windowSize(int a, int b);
now you'd be able to call it with:
windowSize = 800, 600;
or for the getter ( for that to work, D needs to implement tuple return
types):
Stdout(windowSize);

the user should choose what ever notation makes more sense for the
specific case and IMO an IDE should probably default to the function
calling syntax, as it makes sense for all cases while the property
syntax does not.

--Yigal



More information about the Digitalmars-d mailing list