What does this code snippet even do?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Jan 29 22:59:14 UTC 2021


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.


More information about the Digitalmars-d-learn mailing list