Skip to content

Commit

Permalink
Merge pull request #13 from funkwerk/bug11
Browse files Browse the repository at this point in the history
auto return type for accessors
  • Loading branch information
belka-ew authored Oct 20, 2017
2 parents f777386 + eef2834 commit d215349
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 24 deletions.
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ os:
language: d

d:
- dmd-2.073.1
- dmd-2.072.2
- dmd-2.076.1
- dmd-2.075.1
- dmd-2.074.1

env:
matrix:
Expand Down
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"
}
]
}
62 changes: 40 additions & 22 deletions src/accessors.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,22 @@ 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() {"
~ "return [] ~ this.%s;"
~ "}",
visibility, accessorName, 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 +112,13 @@ 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() {"
~ "return [] ~ this.foo;"
~ "}");
}

template GenerateRefReader(string name, alias field)
Expand All @@ -131,11 +130,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 +145,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 +161,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 Expand Up @@ -716,3 +713,24 @@ unittest
mixin(GenerateFieldAccessors);
}
}

/// @Read property returns array with mutable elements.
unittest
{
struct Field
{
}

struct S
{
@Read
Field[] foo_;

mixin(GenerateFieldAccessors);
}

with (S())
{
Field[] arr = foo;
}
}
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);
}
}

0 comments on commit d215349

Please sign in to comment.