-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auto return type for accessors #13
Changes from 2 commits
b08c72f
2c27990
f5424b4
90b07e3
2c368e6
eef2834
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.dub | ||
__test__*__ | ||
/accessors-test-library | ||
/accessors |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,24 +84,24 @@ template GenerateReader(string name, alias field) | |
static enum helper() | ||
{ | ||
import std.string : format; | ||
import std.traits : ForeachType, isArray, isSomeString, MutableOf; | ||
import std.traits : isArray, isSomeString; | ||
|
||
enum visibility = getVisibility!(field, Read); | ||
enum outputType = typeName!(typeof(field)); | ||
enum accessorName = accessor(name); | ||
|
||
static if (isArray!(typeof(field)) && !isSomeString!(typeof(field))) | ||
{ | ||
enum valueType = typeName!(MutableOf!(ForeachType!(typeof(field)))); | ||
|
||
return format("%s final @property inout(%s)[] %s() inout " ~ | ||
"{ inout(%s)[] result = null; result ~= this.%s; return result; }", | ||
visibility, valueType, accessorName, valueType, name); | ||
return format("%s final @property auto %s() inout {" | ||
~ "import std.traits;" | ||
~ "inout(ForeachType!(typeof(this.%s)))[] result = null;" | ||
~ "return result ~ this.%s;" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a more explicit way to copy the array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm aware only of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .dup:
|
||
~ "}", | ||
visibility, accessorName, name, name); | ||
} | ||
else | ||
{ | ||
return format("%s final @property inout(%s) %s() inout { return this.%s; }", | ||
visibility, outputType, accessorName, name); | ||
return format("%s final @property auto %s() inout { return this.%s; }", | ||
visibility, accessorName, name); | ||
} | ||
} | ||
} | ||
|
@@ -114,12 +114,15 @@ unittest | |
int[] intArrayValue; | ||
|
||
static assert(GenerateReader!("foo", integerValue) == | ||
"public final @property inout(int) foo() inout { return this.foo; }"); | ||
"public final @property auto foo() inout { return this.foo; }"); | ||
static assert(GenerateReader!("foo", stringValue) == | ||
"public final @property inout(string) foo() inout { return this.foo; }"); | ||
"public final @property auto foo() inout { return this.foo; }"); | ||
static assert(GenerateReader!("foo", intArrayValue) == | ||
"public final @property inout(int)[] foo() inout " ~ | ||
"{ inout(int)[] result = null; result ~= this.foo; return result; }"); | ||
"public final @property auto foo() inout {" | ||
~ "import std.traits;" | ||
~ "inout(ForeachType!(typeof(this.foo)))[] result = null;" | ||
~ "return result ~ this.foo;" | ||
~ "}"); | ||
} | ||
|
||
template GenerateRefReader(string name, alias field) | ||
|
@@ -131,11 +134,10 @@ template GenerateRefReader(string name, alias field) | |
import std.string : format; | ||
|
||
enum visibility = getVisibility!(field, RefRead); | ||
enum outputType = typeName!(typeof(field)); | ||
enum accessorName = accessor(name); | ||
|
||
return format("%s final @property ref %s %s() { return this.%s; }", | ||
visibility, outputType, accessorName, name); | ||
return format("%s final @property ref auto %s() { return this.%s; }", | ||
visibility, accessorName, name); | ||
} | ||
} | ||
|
||
|
@@ -147,11 +149,11 @@ unittest | |
int[] intArrayValue; | ||
|
||
static assert(GenerateRefReader!("foo", integerValue) == | ||
"public final @property ref int foo() { return this.foo; }"); | ||
"public final @property ref auto foo() { return this.foo; }"); | ||
static assert(GenerateRefReader!("foo", stringValue) == | ||
"public final @property ref string foo() { return this.foo; }"); | ||
"public final @property ref auto foo() { return this.foo; }"); | ||
static assert(GenerateRefReader!("foo", intArrayValue) == | ||
"public final @property ref int[] foo() { return this.foo; }"); | ||
"public final @property ref auto foo() { return this.foo; }"); | ||
} | ||
|
||
template GenerateConstReader(string name, alias field) | ||
|
@@ -163,11 +165,10 @@ template GenerateConstReader(string name, alias field) | |
import std.string : format; | ||
|
||
enum visibility = getVisibility!(field, RefRead); | ||
enum outputType = typeName!(typeof(field)); | ||
enum accessorName = accessor(name); | ||
|
||
return format("%s final @property const(%s) %s() const { return this.%s; }", | ||
visibility, outputType, accessorName, name); | ||
return format("%s final @property auto %s() const { return this.%s; }", | ||
visibility, accessorName, name); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module PersonId; | ||
|
||
class PersonId | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import accessors; | ||
|
||
void main() | ||
{ | ||
} | ||
|
||
// Issue #11: https://github.com/funkwerk/accessors/issues/11 | ||
@nogc nothrow pure @safe unittest | ||
{ | ||
import PersonId : AnotherPersonId = PersonId; | ||
|
||
class PersonId | ||
{ | ||
} | ||
|
||
class Foo | ||
{ | ||
@ConstRead | ||
private AnotherPersonId anotherPersonId_; | ||
|
||
@Read | ||
private AnotherPersonId[] personIdArray_; | ||
|
||
mixin(GenerateFieldAccessors); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Local imports should specify the symbols being imported to avoid hiding local symbols.