digitalmars.D.learn - Defining inout returned values for ranges
- Jonathan Crapuchettes (28/28) Sep 03 2013 If a range struct (Range) is defined inside another struct (Test), how
- anonymous (14/45) Sep 04 2013 You can use a Template This Parameter [1] instead of inout:
- Jonathan Crapuchettes (7/50) Sep 05 2013 Thank you for the help. That worked, but now I ran into another similar
- anonymous (19/43) Sep 05 2013 [...]
- Jonathan Crapuchettes (3/49) Sep 06 2013 Thank you again. It all appears to be working correctly.
If a range struct (Range) is defined inside another struct (Test), how can the constness or mutability of Test be attributed to the return type of Range.front? I'm running into this problem because I need the range to be iterated, but I need the pointer in T to be marked const when appropriate. Thank you, JC Pseudo-Example: struct Test { static struct T { uint* ptr; } ... auto opSlice() inout { static struct Range { inout(T) front() property { ... } ... } return Range(); } }
Sep 03 2013
On Wednesday, 4 September 2013 at 00:56:39 UTC, Jonathan Crapuchettes wrote:If a range struct (Range) is defined inside another struct (Test), how can the constness or mutability of Test be attributed to the return type of Range.front? I'm running into this problem because I need the range to be iterated, but I need the pointer in T to be marked const when appropriate. Thank you, JC Pseudo-Example: struct Test { static struct T { uint* ptr; } ... auto opSlice() inout { static struct Range { inout(T) front() property { ... } ... } return Range(); } }You can use a Template This Parameter [1] instead of inout: ---- auto opSlice(this This)() { static if(is(This == const)) alias QualifiedT = const T; else alias QualifiedT = T; static struct Range { QualifiedT front() property { ---- [1] http://dlang.org/template.html#TemplateThisParameter
Sep 04 2013
On Wed, 04 Sep 2013 11:49:41 +0200, anonymous wrote:On Wednesday, 4 September 2013 at 00:56:39 UTC, Jonathan Crapuchettes wrote:Thank you for the help. That worked, but now I ran into another similar issue. What if the Range struct is being defined outside of the opSlice method? I have a number of methods in a struct that return the same range type and would rather not have to have duplicate code or need to use mixins. JCIf a range struct (Range) is defined inside another struct (Test), how can the constness or mutability of Test be attributed to the return type of Range.front? I'm running into this problem because I need the range to be iterated, but I need the pointer in T to be marked const when appropriate. Thank you, JC Pseudo-Example: struct Test { static struct T { uint* ptr; } ... auto opSlice() inout { static struct Range { inout(T) front() property { ... } ... } return Range(); } }You can use a Template This Parameter [1] instead of inout: ---- auto opSlice(this This)() { static if(is(This == const)) alias QualifiedT = const T; else alias QualifiedT = T; static struct Range { QualifiedT front() property { ---- [1] http://dlang.org/template.html#TemplateThisParameter
Sep 05 2013
On Thursday, 5 September 2013 at 19:19:42 UTC, Jonathan Crapuchettes wrote:On Wed, 04 Sep 2013 11:49:41 +0200, anonymous wrote:[...]Templatize Range and pass it This: ---- static struct Range(This) { static if(is(This == const)) alias QualifiedT = const T; else alias QualifiedT = T; QualifiedT front() property {...} } auto opSlice(this This)() { return Range!This(); } ... more methods in the style of opSlice ... ---- If you need QualifiedT in opSlice, make it a template over This, too.You can use a Template This Parameter [1] instead of inout: ---- auto opSlice(this This)() { static if(is(This == const)) alias QualifiedT = const T; else alias QualifiedT = T; static struct Range { QualifiedT front() property { ---- [1] http://dlang.org/template.html#TemplateThisParameterThank you for the help. That worked, but now I ran into another similar issue. What if the Range struct is being defined outside of the opSlice method? I have a number of methods in a struct that return the same range type and would rather not have to have duplicate code or need to use mixins. JC
Sep 05 2013
On Thu, 05 Sep 2013 23:32:10 +0200, anonymous wrote:On Thursday, 5 September 2013 at 19:19:42 UTC, Jonathan Crapuchettes wrote:Thank you again. It all appears to be working correctly. JCOn Wed, 04 Sep 2013 11:49:41 +0200, anonymous wrote:[...]Templatize Range and pass it This: ---- static struct Range(This) { static if(is(This == const)) alias QualifiedT = const T; else alias QualifiedT = T; QualifiedT front() property {...} } auto opSlice(this This)() { return Range!This(); } ... more methods in the style of opSlice ... ---- If you need QualifiedT in opSlice, make it a template over This, too.You can use a Template This Parameter [1] instead of inout: ---- auto opSlice(this This)() { static if(is(This == const)) alias QualifiedT = const T; else alias QualifiedT = T; static struct Range { QualifiedT front() property { ---- [1] http://dlang.org/template.html#TemplateThisParameterThank you for the help. That worked, but now I ran into another similar issue. What if the Range struct is being defined outside of the opSlice method? I have a number of methods in a struct that return the same range type and would rather not have to have duplicate code or need to use mixins. JC
Sep 06 2013