www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - TypeFunction example: reason about class hierachies

reply Stefan Koch <uplink.coder googlemail.com> writes:
Hi Folks,

Luckily no one uses type functions yet otherwise I would have to 
announce a breaking change.
The type of types changed it's name again from __type to __type__.

Since it turns out that the glibc in.h header uses __type as a 
parameter name already.
Let's hope no one is using __type__.
Because the only option I have after that is 
'__type___make_______bloody_sure____no_one_____uses_this____as_an_identifier'

The code below should be fairly self explanatory.

Please let me know what you think

---
alias type = __type__;
type getSuper(type T)
{
     return __traits(getSuper, T);
}

class B {}
class C : B{}
class D : B {}
bool eq(type A, type B)
{
     return is(A == B);
}

pragma(msg, getSuper(B).eq(getSuper(C)) ); // false
pragma(msg, getSuper(C).eq(B) ); // true
pragma(msg, getSuper(D).eq(getSuper(C)) ); // true
---

P.S. getting is(Myclass SuperType == super) to work, requires 
unreasonably much code.
Since I would need to extend my partial evaluation system ... 
let's not go into it.
I can explain it though if desired.
Besides ... the syntax is one of the things I always look up.
__traits(getSuper) should be more intuitive.
Oct 24 2020
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 24 October 2020 at 20:27:37 UTC, Stefan Koch wrote:
 Please let me know what you think
P.P.S The compiler (from [https://github.com/UplinkCoder/dmd]) supports this starting with the git commit hash e46c6f5bb022aa3d1e7f3208f4b92bd0af9eceb5 onwards. P.P.P.S pragma(msg, getSuper(B)); // "Object" pragma(msg, getSuper(B).getSuper()); // "__emptyType" ragma(msg, getSuper(B).getSuper().getSuper()); // also "__emptyType"
Oct 24 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 24 October 2020 at 20:27:37 UTC, Stefan Koch wrote:
 P.S. getting is(Myclass SuperType == super) to work, requires 
 unreasonably much code.
Is the main blocker there that it only works to make the alias in static if?
Oct 24 2020
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 24 October 2020 at 20:54:45 UTC, Adam D. Ruppe wrote:
 On Saturday, 24 October 2020 at 20:27:37 UTC, Stefan Koch wrote:
 P.S. getting is(Myclass SuperType == super) to work, requires 
 unreasonably much code.
Is the main blocker there that it only works to make the alias in static if?
The main blocker is that it that `SuperType` is Identifier, I would have to change the way Is Expressions are represented. And inject a variable into the scope. It's quite messy to do that.
Oct 24 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 24 October 2020 at 21:01:19 UTC, Stefan Koch wrote:
 The main blocker is that it that `SuperType` is Identifier, I 
 would have to change the way Is Expressions are represented.
 And inject a variable into the scope.
They already do that, so why can't you just use the existing code?
Oct 24 2020
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 24 October 2020 at 22:59:00 UTC, Adam D. Ruppe wrote:
 On Saturday, 24 October 2020 at 21:01:19 UTC, Stefan Koch wrote:
 The main blocker is that it that `SuperType` is Identifier, I 
 would have to change the way Is Expressions are represented.
 And inject a variable into the scope.
They already do that, so why can't you just use the existing code?
Because that only works when you have a new socpe. I _could_ inject a fake variable but it's a really nasty thing to do, since it's an AST change. I do not want to change the AST of the function. Really not.
Oct 24 2020
prev sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 24 October 2020 at 22:59:00 UTC, Adam D. Ruppe wrote:
 On Saturday, 24 October 2020 at 21:01:19 UTC, Stefan Koch wrote:
 The main blocker is that it that `SuperType` is Identifier, I 
 would have to change the way Is Expressions are represented.
 And inject a variable into the scope.
They already do that, so why can't you just use the existing code?
Also why would I support such un-intuitive syntax? Are there any reasons other than backwards compatibility?
Oct 24 2020