Thread overview |
---|
February 18, 2018 std.traits.isBoolean | ||||
---|---|---|---|---|
| ||||
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? |
February 18, 2018 Re: std.traits.isBoolean | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | 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"); } ===== |
February 19, 2018 Re: std.traits.isBoolean | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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 |
February 19, 2018 Re: std.traits.isBoolean | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | 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) |
February 19, 2018 Re: std.traits.isBoolean | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | 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?
|
February 19, 2018 Re: std.traits.isBoolean | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tony | 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)`.
|
February 20, 2018 Re: std.traits.isBoolean | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nathan S. | 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;" |
Copyright © 1999-2021 by the D Language Foundation