项目作者: funkwerk

项目描述 :
Generate D getters and setters automatically
高级语言: D
项目地址: git://github.com/funkwerk/accessors.git
创建时间: 2016-12-07T22:50:19Z
项目社区:https://github.com/funkwerk/accessors

开源协议:Boost Software License 1.0

下载


accessors

Build Status
License
Dub Version
codecov

accessors module allows to generate getters and setters automatically.

Deprecation warning: accessors has been succeeded by boilerplate.

Usage

  1. import accessors;
  2. class WithAccessors
  3. {
  4. @Read @Write
  5. private int num_;
  6. mixin(GenerateFieldAccessors);
  7. }

@Read and @Write generate the following two methods:

  1. public final @property auto num() inout @nogc nothrow pure @safe
  2. {
  3. return this.num_;
  4. }
  5. public final @property void num(int num) @nogc nothrow pure @safe
  6. {
  7. this.num_ = num;
  8. }

The accessors names are derived from the appropriate member variables by
removing an underscore from the beginning or the end of the variable name.

Available user defined attributes

As you can see there are multiple attributes that can be used to generate
getters and only one for the setters. The getters differ by the return
type. @Read returns an inout value, @ConstRead - a const value.

Accessor visibility

Visibility of the generated accessors is by default public, but it can be
changed. In order to achieve this, you have to pass public, protected,
private or package as argument to the attribute:

  1. import accessors;
  2. class WithAccessors
  3. {
  4. @Read("public") @Write("protected")
  5. private int num_;
  6. mixin(GenerateFieldAccessors);
  7. }

Example

  1. import accessors;
  2. import std.stdio;
  3. class Person
  4. {
  5. @Read @Write
  6. private uint age_;
  7. @ConstRead
  8. private string name_;
  9. this(in string name, in uint age = 0)
  10. {
  11. this.name_ = name;
  12. this.age_ = age;
  13. }
  14. mixin(GenerateFieldAccessors);
  15. }
  16. void main()
  17. {
  18. auto person = new Person("Saul Kripke");
  19. person.age = 57;
  20. writeln(person.name, ": ", person.age);
  21. }

Bugs

If you experience compile-time problems, open an
issue with the
information about the type of the member variable fails.