@property - take it behind the woodshed and shoot it?

Borislav Kosharov boby_dsm at abv.bg
Sat Aug 10 09:26:28 PDT 2013


On Saturday, 10 August 2013 at 10:29:51 UTC, Stian Pedersen wrote:
> To add to the mess - or maybe suggest a new approach, what 
> about:
>
> class A
> {
>     int foo();
>     void foo=(int a);
>     private foo_;
> }
>
> Then a.foo = 42; calls the foo= method. No other conversions 
> from a=b to a method invocation.
>
> It may be suggested in one of these 46 pages which I haven't 
> read. And it'll probably break a lot of stuff.

I like this. It is something like in Ruby. And Ruby also has some 
meta programming support and there are these 3 "templates" 
attr_reader, attr_writer and attr_accessor (both of the previous 
together). And one could write (in Ruby):

class Foo
     attr_writer :name, :number #Ruby has symbols(something like 
enums)
end

And translates to:

class Foo
     def name=(name)
         @name = name //the @name means class private variable in 
Ruby
     end
     def number=(number)
         @number = number //and they don't need to be 
declared(dynamicly typed lang)
     end
end

So in D those 3 attr 'shortcuts' could be implemented with mixin 
templates. And instead of those 'symbols' they could be just a 
list of strings. Example:

class Foo {
     mixin Reader!("name", "number");
}


And something similar to happen. Although this not solves all 
@property problems it makes writing them shorter. And I have 
started experimenting with that and it is almost finished, but it 
is a little different from what I described above. The file is a 
part of a thing I started working on in D, but here is a direct 
link(https://github.com/nikibobi/spine-d/blob/master/src/spine/util/property.d) 
and the files is isolated from the other things. See the 
unittests for examples.


More information about the Digitalmars-d mailing list