www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I just discovered an alternative use of the `in`-operator

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I just discovered that the is-operator can be used as in

template ElementType(R)
{
     static if (is(typeof(R.init.front.init) T))
         alias ElementType = T;
     else
         alias ElementType = void;
}

. Very powerful. Is this documented?
Aug 06 2020
next sibling parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Thursday, 6 August 2020 at 21:50:06 UTC, Per Nordlöw wrote:
 I just discovered that the is-operator can be used as in
 ...
Doh, title should of course be I just discovered an alternative use of `is`-expressions.
Aug 06 2020
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Thursday, 6 August 2020 at 21:50:06 UTC, Per Nordlöw wrote:
 I just discovered that the is-operator can be used as in

 template ElementType(R)
 {
     static if (is(typeof(R.init.front.init) T))
         alias ElementType = T;
     else
         alias ElementType = void;
 }

 . Very powerful. Is this documented?
Yes, in the following paragraph under "IsExpression" [1]:
 Identifier is declared to be an alias of the resulting type if 
 the condition is satisfied. The Identifier forms can only be 
 used if the IsExpression appears in a StaticIfCondition.
[1] https://dlang.org/spec/expression.html#is_expression
Aug 06 2020
next sibling parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Thursday, 6 August 2020 at 22:24:43 UTC, Paul Backus wrote:
 [1] https://dlang.org/spec/expression.html#is_expression
Thanks
Aug 07 2020
prev sibling next sibling parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Thursday, 6 August 2020 at 22:24:43 UTC, Paul Backus wrote:
 [1] https://dlang.org/spec/expression.html#is_expression
I bet there a several places in Phobos where this feature isn't but could be used.
Aug 07 2020
next sibling parent aberba <karabutaworld gmail.com> writes:
On Friday, 7 August 2020 at 21:02:03 UTC, Per Nordlöw wrote:
 On Thursday, 6 August 2020 at 22:24:43 UTC, Paul Backus wrote:
 [1] https://dlang.org/spec/expression.html#is_expression
I bet there a several places in Phobos where this feature isn't but could be used.
I feel same. That there's a clever use of certain D features waiting to be discovered.
Aug 07 2020
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Aug 07, 2020 at 09:02:03PM +0000, Per Nordlöw via Digitalmars-d-learn
wrote:
 On Thursday, 6 August 2020 at 22:24:43 UTC, Paul Backus wrote:
 [1] https://dlang.org/spec/expression.html#is_expression
I bet there a several places in Phobos where this feature isn't but could be used.
Perhaps. But as I recall, it's widely used throughout Phobos, so it probably won't be easy to find a place where it could be used but isn't. T -- Written on the window of a clothing store: No shirt, no shoes, no service.
Aug 07 2020
prev sibling parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Thursday, 6 August 2020 at 22:24:43 UTC, Paul Backus wrote:
 [1] https://dlang.org/spec/expression.html#is_expression
Thanks
Aug 09 2020