Is there a d-lang equivalent of Ruby's attr_accessor -
In Ruby I can easily read / write properties on a square like this:
< Code> class bar attr_accessor: foo ,: bizz,: buzz endthis will be the same
class bar def foo @foo end def foo = (Foo ) @fu = EFU and DEF buzz @ FU and DEF BIS = (Bizz) @ BIS = Biz and DEF buzz @ Buzz and Diff buzz = (buzz) @ Buzz = Buzz End End
The same code D is very much workbook, and I repeat things to all the places myself:
class bar {free G {string _foo, _bizz, _buzz; } @property {string foo () {return _fu; } String biz () {return_bizz; } String buzz () {return _buzz; } Zero foo (string foo) {_foo = foo; } Zero biz (string biz) {_bizz = bizz; } Zero buzz (string buzz) {_buzz = buzz; }}}
Is there a short way to do this?
Yes:
public string foo;
The insignificant property accessory time is a waste of IMO. Ruby forces you to do them but D does not.
If you want them anyway, a mixin template can do the job:
mixin template attr_accessor (type, string name) {static string code generation helper () {String code; // variable code ~ ~ "private type _" ~ name ~ ";"; // recipient code ~ = "public @ property type" ~ name ~ "() {back _" ~ name ~ ";}"; // setter code ~ = "public @ property type" ~ name ~ "(type) {return _" ~ name ~ "= a;}"; return code; } Mixin (Code GenerationHelper ()); } // here square foo {mixin attr_accessor! (String, "foo") is used; } Zero main () {auto foo = new Foo (); Foo.foo = "test"; }
Comments
Post a Comment