Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 29, 2021 What does this code snippet even do? | ||||
---|---|---|---|---|
| ||||
// 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? |
January 29, 2021 Re: What does this code snippet even do? | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry | 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. |
January 30, 2021 Re: What does this code snippet even do? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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 👍
|
January 29, 2021 Re: What does this code snippet even do? | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry | On 1/29/21 2:41 PM, WhatMeWorry wrote: > Ali's book talks about the > 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 syntax as is (T : Specifier, TemplateParamList) --- quote --- 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. ------------ I almost understand it. ;) Ali |
Copyright © 1999-2021 by the D Language Foundation