The "type" type dream

Nick Sabalausky a at a.a
Wed Nov 26 13:01:19 PST 2008


"Eric Poggel" <fake at example.com> wrote in message 
news:492D8B95.9020100 at example.com...
>
> Seconded.
>
> What if we treat Type like a class and store all of the type specific 
> meta-data as properties of it?
>
> int a;
> Type T = a.type; // or valueTypeOf(a), whatever syntax you prefer.
> writefln(T.sizeof); // writes 4
>
> Type could even have it's own set of subclasses for dealing with specific 
> types of data.  We could have type info classes for Primitive, Struct, 
> Class, Union, etc. that all inherit from Type.
>

I agree with all of this.

> class A {
> void foo(){}
> void bar(){}
> };
> A a;
> Class C = a.type; // Class inherits from Type
> foreach (M; C.publicMethods)
> if (M.type == A.foo.type)
> writefln(M.name); // writes "foo")
>
> // We could also do things like this, if the stack would allow it.
> a.type b = new a.type;
>
> It's probably quite a pipe dream to be able to do this outside a scripting 
> language:
>
> void foo() { writefln("bar")};
> Class C = new Class();
> C.addMethod(&foo);
> auto c = new C();
> c.foo(); // writes "bar"
>
> Finally, being able to work with types in this manner could eliminate 
> most/all of the other competing syntaxes.  Of course, I haven't spent much 
> thought on the actual implementation, which could be quite 
> hard/impossible.

I'm not convinced I would want to be able to do that outside a scripting 
language (or even in one, for that matter). Making type definitions mutable 
at run-time breaks the concept of static typing. And I like static typing. I 
think C#-style extension methods are a much better way to get a similar 
effect.





More information about the Digitalmars-d mailing list