digitalmars.D.learn - Conditional Inheritance
- JS (3/3) Jul 13 2013 I need to conditionally inherit:
- JS (4/4) Jul 13 2013 and while I'm at it I need to conditionally constrain types.
- lomereiter (2/6) Jul 13 2013 interface A(T) if (isBasicType!T || is(T : B))
- JS (3/11) Jul 13 2013 Thanks, works. I remember seeing code like this somewhere but
- Simen Kjaeraas (6/17) Jul 14 2013 Here. I admit there should probably be more examples, and perhaps also
- JS (2/20) Jul 14 2013 thanks.
- lomereiter (7/7) Jul 13 2013 This should work:
- JS (6/13) Jul 13 2013 Thanks, I tried something similar but it didn't work right...
- lomereiter (2/4) Jul 14 2013 Yes, it does. Once ':' is typed, some inheritance must occur.
- Timon Gehr (9/13) Jul 14 2013 Nope.
- lomereiter (2/2) Jul 14 2013 Thanks.
- Simen Kjaeraas (12/15) Jul 14 2013 template conditionallyInherit(bool choice, T...) {
I need to conditionally inherit: interface A(T) : conditionallyInherit!(isBasicType!T, B); A!(double) will inherit B but A!(mytype) won't.
Jul 13 2013
and while I'm at it I need to conditionally constrain types. interface A(T) where(!isBasicType!T, (T : B)); which is suppose to require T to inherit from B if T is not basic type.
Jul 13 2013
On Sunday, 14 July 2013 at 05:04:37 UTC, JS wrote:and while I'm at it I need to conditionally constrain types. interface A(T) where(!isBasicType!T, (T : B)); which is suppose to require T to inherit from B if T is not basic type.interface A(T) if (isBasicType!T || is(T : B))
Jul 13 2013
On Sunday, 14 July 2013 at 05:30:57 UTC, lomereiter wrote:On Sunday, 14 July 2013 at 05:04:37 UTC, JS wrote:Thanks, works. I remember seeing code like this somewhere but couldn't find any docs about it.and while I'm at it I need to conditionally constrain types. interface A(T) where(!isBasicType!T, (T : B)); which is suppose to require T to inherit from B if T is not basic type.interface A(T) if (isBasicType!T || is(T : B))
Jul 13 2013
On 2013-07-14, 07:40, JS wrote:On Sunday, 14 July 2013 at 05:30:57 UTC, lomereiter wrote:Here. I admit there should probably be more examples, and perhaps also some info on the classes/interfaces/structs & unions pages. http://dlang.org/template.html#Constraint -- SimenOn Sunday, 14 July 2013 at 05:04:37 UTC, JS wrote:Thanks, works. I remember seeing code like this somewhere but couldn't find any docs about it.and while I'm at it I need to conditionally constrain types. interface A(T) where(!isBasicType!T, (T : B)); which is suppose to require T to inherit from B if T is not basic type.interface A(T) if (isBasicType!T || is(T : B))
Jul 14 2013
On Sunday, 14 July 2013 at 09:52:29 UTC, Simen Kjaeraas wrote:On 2013-07-14, 07:40, JS wrote:thanks.On Sunday, 14 July 2013 at 05:30:57 UTC, lomereiter wrote:Here. I admit there should probably be more examples, and perhaps also some info on the classes/interfaces/structs & unions pages. http://dlang.org/template.html#ConstraintOn Sunday, 14 July 2013 at 05:04:37 UTC, JS wrote:Thanks, works. I remember seeing code like this somewhere but couldn't find any docs about it.and while I'm at it I need to conditionally constrain types. interface A(T) where(!isBasicType!T, (T : B)); which is suppose to require T to inherit from B if T is not basic type.interface A(T) if (isBasicType!T || is(T : B))
Jul 14 2013
This should work: template conditionallyInherit(bool inherit, T) { static if (inherit) alias T conditionallyInherit; else interface conditionallyInherit {} }
Jul 13 2013
On Sunday, 14 July 2013 at 05:28:08 UTC, lomereiter wrote:This should work: template conditionallyInherit(bool inherit, T) { static if (inherit) alias T conditionallyInherit; else interface conditionallyInherit {} }Thanks, I tried something similar but it didn't work right... I assume that you template ends up creating a dummy interface though and this isn't really acceptable. e.g., interface A : conditionallyInherit!(false, B) { } will cause A to inherit from conditionallyInherit?
Jul 13 2013
I assume that you template ends up creating a dummy interface though and this isn't really acceptable.Yes, it does. Once ':' is typed, some inheritance must occur. Why isn't a dummy interface acceptable?
Jul 14 2013
On 07/14/2013 11:37 AM, lomereiter wrote:Nope. template Seq(T...){ alias T Seq; } class C : Seq!(){ }I assume that you template ends up creating a dummy interface though and this isn't really acceptable.Yes, it does. Once ':' is typed, some inheritance must occur.Why isn't a dummy interface acceptable?It creates unnecessary bloat. template conditionallyInherit(bool inherit, T) { static if(inherit) alias T conditionallyInherit; else alias Seq!() conditionallyInherit; }
Jul 14 2013
Thanks. Now I realise that D is much less intuitive than C++.
Jul 14 2013
On 2013-07-14, 07:00, JS wrote:I need to conditionally inherit: interface A(T) : conditionallyInherit!(isBasicType!T, B); A!(double) will inherit B but A!(mytype) won't.template conditionallyInherit(bool choice, T...) { static if (choice) { alias conditionallyInherit = T; } else { import std.typetuple : TypeTuple; alias conditionallyInherit = TypeTuple!(); } } Should work. -- Simen
Jul 14 2013