www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What does this code snippet even do?

reply WhatMeWorry <kheaser gmail.com> writes:
// The following four lines in run.lang.io

int[] a;
alias T = long;
pragma(msg, is(typeof(a) : U[], U : T));
pragma(msg, is(typeof(a) : T[]));

// returns

true
false

But I'm not even sure what I'm looking at.  Ali's book talks 
about the colon appearing for
:, associative array   ⬁
:, import   ⬁
:, inheritance   ⬁
:, label
but I'm pretty sure none apply here.

I know about alias (T is replaced with long), pragma, is, and 
typeof. But what is U and where does it come from?  And what do 
the colons do here?
Jan 29
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Jan 29, 2021 at 10:41:33PM +0000, WhatMeWorry via Digitalmars-d-learn
wrote:
 // The following four lines in run.lang.io
 
 int[] a;
 alias T = long;
 pragma(msg, is(typeof(a) : U[], U : T));
This means: "does the type of 'a' have the form U[], where U is a type that implicitly converts to T?".
 pragma(msg, is(typeof(a) : T[]));
This means: "does the type of 'a' implicitly convert to T[]?". [...]
 I know about alias (T is replaced with long), pragma, is, and typeof.
 But what is U and where does it come from?  And what do the colons do
 here?
Colon means "implicitly converts to". U is a template parameter to an implicit template `U[]`. It's basically used for pattern-matching the LHS type to some type pattern on the RHS. The general pattern is: is(typeToBeMatched : typePattern, templateParams...) `typeToBeMatched` is treated as a argument type to be matched against `typePattern` as if it were a template with parameters `templateParams`. T -- Designer clothes: how to cover less by paying more.
Jan 29
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Friday, 29 January 2021 at 22:59:14 UTC, H. S. Teoh wrote:
 On Fri, Jan 29, 2021 at 10:41:33PM +0000, WhatMeWorry via 
 Digitalmars-d-learn wrote:
 [...]
This means: "does the type of 'a' have the form U[], where U is a type that implicitly converts to T?".
 [...]
This means: "does the type of 'a' implicitly convert to T[]?". [...]
 [...]
Colon means "implicitly converts to". U is a template parameter to an implicit template `U[]`. It's basically used for pattern-matching the LHS type to some type pattern on the RHS. The general pattern is: is(typeToBeMatched : typePattern, templateParams...) `typeToBeMatched` is treated as a argument type to be matched against `typePattern` as if it were a template with parameters `templateParams`. T
I wish we could upvote answers 👍
Jan 29
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 1/29/21 2:41 PM, WhatMeWorry wrote:

=C2=A0 Ali's book talks about the=20
 colon appearing for
There is also "is, expression": http://ddili.org/ders/d.en/is_expr.html#ix_is_expr.is,%20expression But the is expression is so complicated. :( I defined that particular=20 syntax as is (T : Specifier, TemplateParamList) --- quote --- identifier, Specifier, :, and =3D=3D all have the same meanings as descri= bed=20 above. TemplateParamList is both a part of the condition that needs to be=20 satisfied and a facility to define additional aliases if the condition=20 is indeed satisfied. It works in the same way as template type deduction.= ------------ I almost understand it. ;) Ali
Jan 29