Skip to content
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

Merged
merged 6 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.dub
__test__*__
/accessors-test-library
/accessors
6 changes: 6 additions & 0 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"configurations": [
{
"name": "library"
},
{
"name": "unittest",
"targetType": "executable",
"sourcePaths": ["test"],
"mainSourceFile": "test/main.d"
}
]
}
45 changes: 23 additions & 22 deletions src/accessors.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
Copy link

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.

~ "inout(ForeachType!(typeof(this.%s)))[] result = null;"
~ "return result ~ this.%s;"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more explicit way to copy the array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware only of .dup.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try .dup; otherwise, try [] ~ ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.dup:

src/accessors.d-mixin-666-mixin-668(668,58): Error: template object.dup cannot deduce function from argument types !()(inout(const(X)[])), candidates are:
/usr/include/dmd/druntime/import/object.d(1943,6):        object.dup(T : V[K], K, V)(T aa)
/usr/include/dmd/druntime/import/object.d(1979,6):        object.dup(T : V[K], K, V)(T* aa)
/usr/include/dmd/druntime/import/object.d(3737,16):        object.dup(T)(T[] a) if (!is(const(T) : T))
/usr/include/dmd/druntime/import/object.d(3753,15):        object.dup(T)(const(T)[] a) if (is(const(T) : T))
src/accessors.d(677,9): Error: static assert  is(typeof(y) == const(X)[]) is false

~ "}",
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);
}
}
}
Expand All @@ -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)
Expand All @@ -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);
}
}

Expand All @@ -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)
Expand All @@ -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);
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/PersonId.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module PersonId;

class PersonId
{
}
26 changes: 26 additions & 0 deletions test/main.d
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);
}
}