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 end   

this 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

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -