accessors 1.2.3
D getter/setter generator
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
accessors
accessors
module allows to generate getters and setters automatically.
Usage
import accessors;
class WithAccessors
{
@Read @Write
private int num_;
mixin(GenerateFieldAccessors);
}
@Read
and @Write
generate the following two methods:
public final @property auto num() inout @nogc nothrow pure @safe
{
return this.num_;
}
public final @property void num(int num) @nogc nothrow pure @safe
{
this.num_ = num;
}
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
@Read
@ConstRead
@Write
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:
import accessors;
class WithAccessors
{
@Read("public") @Write("protected")
private int num_;
mixin(GenerateFieldAccessors);
}
Example
import accessors;
import std.stdio;
class Person
{
@Read @Write
private uint age_;
@ConstRead
private string name_;
this(in string name, in uint age = 0)
{
this.name_ = name;
this.age_ = age;
}
mixin(GenerateFieldAccessors);
}
void main()
{
auto person = new Person("Saul Kripke");
person.age = 57;
writeln(person.name, ": ", person.age);
}
Bugs
If you experience compile-time problems, open an issue with the information about the type of the member variable fails.
- Registered by Eugene Wissner
- 1.2.3 released 6 years ago
- funkwerk/accessors
- BSL-1.0
- Authors:
- Dependencies:
- none
- Versions:
-
1.2.4 2018-Jan-23 1.2.3 2018-Jan-19 1.2.2 2018-Jan-12 1.2.1 2018-Jan-11 1.2.0 2017-Dec-13 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
415 downloads total
-
- Score:
- 1.9
- Short URL:
- accessors.dub.pm