www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Detecting value parameter of template instance in "is" expression

reply "Uranuz" <neuranuz gmail.com> writes:
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
parent reply "anonymous" <anonymous example.com> writes:
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
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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:
 How should I define value parameters inside "is" expression to make code
 like this working?
is( N == Nullable!(T), T ... )
Or is( N == Nullable!(T, nV), T, alias nV ) Since nV is most probably an alias template parameter.
Dec 05 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/05/2013 01:00 PM, Philippe Sigaud wrote:

 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:
 How should I define value parameters inside "is" expression to make 
code
 like this working?
is( N == Nullable!(T), T ... )
Or is( N == Nullable!(T, nV), T, alias nV ) Since nV is most probably an alias template parameter.
But 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.html
Dec 05 2013
parent reply "Uranuz" <neuranuz gmail.com> writes:
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
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
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