digitalmars.D.learn - Detecting value parameter of template instance in "is" expression
- Uranuz (30/30) Dec 05 2013 I'm trying to detect values that were instantiated from
- anonymous (2/28) Dec 05 2013 is( N == Nullable!(T), T ... )
- Philippe Sigaud (4/8) Dec 05 2013 Or
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (24/34) Dec 05 2013 But none of those answer what I think the OP is asking: T must be a type...
- Uranuz (5/5) Dec 05 2013 As far as I understand TemplateParamList in *is* expr should work
- Philippe Sigaud (22/26) Dec 05 2013 Once you are 'inside' a static if guarded by an is() expression, the
I'm trying to detect values that were instantiated from std.typecons.Nullable (and NullableRef). And I will net to get base types of these Nullable values. For simplicity I write the following code to test for Nullable. //------------------- import std.stdio, std.typecons; template isStdNullable(N) { static if( is( N == Nullable!(T), T ) || is( N == NullableRef!(T), T ) || is( N == Nullable!(T, nV), T, T nV ) //There is an error ) enum bool isNullable = true; else enum bool isNullable = false; } void main() { writeln(isStdNullable!(Nullable!(int, 10))); } //----------------- Compilation output: /d966/f969.d(8): Error: undefined identifier T, did you mean alias N? /d966/f969.d(19): Error: template instance f969.isStdNullable!(Nullable!(int, 10)) error instantiating How should I define value parameters inside "is" expression to make code like this working?
Dec 05 2013
On Thursday, 5 December 2013 at 20:07:51 UTC, Uranuz wrote://------------------- import std.stdio, std.typecons; template isStdNullable(N) { static if( is( N == Nullable!(T), T ) || is( N == NullableRef!(T), T ) || is( N == Nullable!(T, nV), T, T nV ) //There is an error ) enum bool isNullable = true; else enum bool isNullable = false; } void main() { writeln(isStdNullable!(Nullable!(int, 10))); } //----------------- Compilation output: /d966/f969.d(8): Error: undefined identifier T, did you mean alias N? /d966/f969.d(19): Error: template instance f969.isStdNullable!(Nullable!(int, 10)) error instantiating How should I define value parameters inside "is" expression to make code like this working?is( N == Nullable!(T), T ... )
Dec 05 2013
On Thu, Dec 5, 2013 at 9:17 PM, anonymous <anonymous example.com> wrote:On Thursday, 5 December 2013 at 20:07:51 UTC, Uranuz wrote:Or is( N == Nullable!(T, nV), T, alias nV ) Since nV is most probably an alias template parameter.How should I define value parameters inside "is" expression to make code like this working?is( N == Nullable!(T), T ... )
Dec 05 2013
On 12/05/2013 01:00 PM, Philippe Sigaud wrote:On Thu, Dec 5, 2013 at 9:17 PM, anonymous <anonymous example.com> wrote:codeOn Thursday, 5 December 2013 at 20:07:51 UTC, Uranuz wrote:How should I define value parameters inside "is" expression to makeBut none of those answer what I think the OP is asking: T must be a type AND nV must be a value of that type. I had thought the following to be true (especially the last quoted sentence) [1]: <quote> is (T : Specifier, TemplateParamList) is (T == Specifier, TemplateParamList) is (T identifier : Specifier, TemplateParamList) is (T identifier == Specifier, TemplateParamList) These syntaxes allow for more complex cases. identifier may be omitted. identifier, Specifier, :, and == all have the same meanings as described above. TemplateParamList is both a part of the condition that needs to be satisfied and a facility to define additional aliases if the condition is indeed satisfied. It works in the same way as template type deduction. </quote> If my the last sentence of mine is correct, then this is an implementation problem as Uranuz's syntax should work. Although, I don't see why my sentence would not be true. It definitely to be so in many other uses. Ali [1] http://ddili.org/ders/d.en/is_expr.htmlOr is( N == Nullable!(T, nV), T, alias nV ) Since nV is most probably an alias template parameter.like this working?is( N == Nullable!(T), T ... )
Dec 05 2013
As far as I understand TemplateParamList in *is* expr should work just like it works in usual template. So this problem is just lack of implementation. I'll try to find workaround with * is( N == Nullable!(T), T ... ) * syntax. Thank you for response.
Dec 05 2013
On Fri, Dec 6, 2013 at 5:31 AM, Uranuz <neuranuz gmail.com> wrote:As far as I understand TemplateParamList in *is* expr should work just like it works in usual template. So this problem is just lack of implementation. I'll try to find workaround with * is( N == Nullable!(T), T ... ) * syntax. Thank you for response.Once you are 'inside' a static if guarded by an is() expression, the symbols you introduced inside the is() are visible, so you can test T... further if you want. In my proposal, nV is visible right after the is(), so here is a more correct expression: import std.stdio, std.typecons; template isStdNullable(N) { static if( is( N == Nullable!(T), T ) || is( N == NullableRef!(T), T ) || is( N == Nullable!(T, nV), T, alias nV ) && is(typeof(nV) == T) // <-- There ) enum bool isStdNullable = true; else enum bool isStdNullable = false; } void main() { writeln(isStdNullable!(Nullable!(int, 10))); }
Dec 05 2013