www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.traits.isBoolean

reply Tony <tonytdominguez aol.com> writes:
At
https://dlang.org/library/std/traits/is_boolean.html

it has:


enum isBoolean(T) = is(BooleanTypeOf!T) && !isAggregateType!T;

per:
https://dlang.org/library/std/traits/is_aggregate_type.html

isAggregateType is true for [struct, union, class, interface].

So BooleanTypeOf!T is true for structs, unions, classes and 
interfaces? And if yes, why is that so?
Feb 18 2018
parent reply Mike Parker <aldacron gmail.com> writes:
On Sunday, 18 February 2018 at 14:52:37 UTC, Tony wrote:
 At
 https://dlang.org/library/std/traits/is_boolean.html

 it has:


 enum isBoolean(T) = is(BooleanTypeOf!T) && !isAggregateType!T;

 per:
 https://dlang.org/library/std/traits/is_aggregate_type.html

 isAggregateType is true for [struct, union, class, interface].

 So BooleanTypeOf!T is true for structs, unions, classes and 
 interfaces? And if yes, why is that so?
Generally, no. But with alias this, it can be: ===== import std.traits : BooleanTypeOf; import std.stdio : writeln; struct NoBool { int x; } struct AliasThisBool { bool b; alias b this; } void main() { static if(is(BooleanTypeOf!NoBool)) writeln("NoBool"); static if(is(BooleanTypeOf!AliasThisBool)) writeln("AliasThisBool"); } =====
Feb 18 2018
parent reply Tony <tonytdominguez aol.com> writes:
On Sunday, 18 February 2018 at 15:12:50 UTC, Mike Parker wrote:

 Generally, no. But with alias this, it can be:

 =====
 import std.traits : BooleanTypeOf;
 import std.stdio : writeln;

 struct NoBool {
     int x;
 }

 struct AliasThisBool {
     bool b;
     alias b this;
 }

 void main()
 {
     static if(is(BooleanTypeOf!NoBool)) writeln("NoBool");
     static if(is(BooleanTypeOf!AliasThisBool)) 
 writeln("AliasThisBool");
 }
Thanks! It doesn't appear that BooleanTypeof is documented on dlang.org (outside of it's placement on the isBooleanType page). At least it isn't coming up in a "BooleanTypeOf site:dlang.org" search and not on the traits page: https://dlang.org/library/std/traits.html
Feb 19 2018
parent reply Basile B. <b2.temp gmx.com> writes:
On Monday, 19 February 2018 at 13:07:08 UTC, Tony wrote:
 It doesn't appear that BooleanTypeof is documented on dlang.org 
 (outside of it's placement on the isBooleanType page). At least 
 it isn't coming up in a "BooleanTypeOf site:dlang.org" search 
 and not on the traits page:

 https://dlang.org/library/std/traits.html
Indeed but Phobos maintainers don't want the ...TypeOf family to be documented. (https://github.com/dlang/phobos/pull/5747)
Feb 19 2018
parent reply Tony <tonytdominguez aol.com> writes:
On Monday, 19 February 2018 at 13:47:15 UTC, Basile B. wrote:

 Indeed but Phobos maintainers don't want the ...TypeOf family 
 to be documented.
 (https://github.com/dlang/phobos/pull/5747)
Ok, thanks. But, assuming there is a use case for it, what if you want to restrict to a type that is either boolean, or a struct/class that can substitute for boolean - how do you do that without using the "private" TypeOfBoolean thing?
Feb 19 2018
parent reply Nathan S. <no.public.email example.com> writes:
On Monday, 19 February 2018 at 15:12:15 UTC, Tony wrote:
 But, assuming there is a use case for it, what if you want to 
 restrict to a type that is either boolean, or a struct/class 
 that can substitute for boolean - how do you do that without 
 using the "private" TypeOfBoolean thing?
In that case you can just write `is(T : bool)`.
Feb 19 2018
parent Tony <tonytdominguez aol.com> writes:
On Monday, 19 February 2018 at 17:22:04 UTC, Nathan S. wrote:
 On Monday, 19 February 2018 at 15:12:15 UTC, Tony wrote:
 But, assuming there is a use case for it, what if you want to 
 restrict to a type that is either boolean, or a struct/class 
 that can substitute for boolean - how do you do that without 
 using the "private" BooleanTypeOf thing?
In that case you can just write `is(T : bool)`.
Thanks. Assuming it would substitute, that should probably be used on this page in place of BooleanTypeOf since BooleanTypeOf is not supposed to be public: https://dlang.org/library/std/traits/is_boolean.html "enum isBoolean(T) = is(BooleanTypeOf!T) && !isAggregateType!T;"
Feb 19 2018