#318 Magic 'type' method

tompalmer Fri 25 Jul 2008

It occurred to me that type is magic, in the sense that it's either virtual or static depending on how you call it:

fansh> Obj o := "hello"
hello
fansh> o.type
sys::Str
fansh> Obj.type
sys::Obj

I think it would be better to have two different methods, such as type (static) and thisType (virtual). (Alternatively, it could be staticType and type, depending on your preference for which is static vs. virtual.) Also, just as in Java, static methods should be able to clash with other static methods in super classes.

Then static type effectively just becomes a compiler generated static method on every type. (Maybe not implemented this way, but that's the effect.)

This will also make the new simplified logging API easier to get right.

(Note that in Java the difference is also explicit but a bit unclear given the Type.class vs. obj.getClass() alternatives.)

On an unrelated note, I'll likely have limited computer access for the next few weeks, so I might not be as active/helpful/bothersome as usual on here for a while.

alexlamsl Fri 25 Jul 2008

Oh that's strangely magical indeed - I might have fall for Str.type -> Type if I see s.type -> Str

jodastephen Sat 26 Jul 2008

I wouldn't change this especially, even though I can see the point being made. I would however consider whether Fan should have static methods/state at all - but thats probably for another thread on another day.

brian Sat 26 Jul 2008

Just to clarify:

Str.type  =>  type literal
x.type    =>  calls Obj.type()

Type literals aren't static methods, they just literals handled by the compiler like number or string literals.

I personally like the syntax, and don't think it is confusing - I expect just about any Java programmer would get it pretty quickly. But I see your point.

My main problem with the current syntax is that I also want slot literals. Today if I want to lookup a Method:

m := Str.type.method("replace")

What I really want to do is something like:

m := Str.replace

Obviously that syntax doesn't work because it conflicts with static field access. So ideally I'd like a new unified syntax that works for both type literals and slot literals. I've been thinking of something like #:

Str#replace

But that still doesn't really work for type literals:

Str#type => really means => Str.type.method("type")

Slot literals are a feature I want to for 1.0, so I'd really like suggestions for proposed syntax.

alexlamsl Sat 26 Jul 2008

If we are talking about slot literals, I would love to have Str#replace.

As for this topic - I have a Really Bad Idea (TM):

Str.type  // Type
Str.Type  // Str

Str s = "hello world!";
s.type    // Str
s.Type    // compile-time error

brian Sun 27 Jul 2008

Using Type with a capital is an interesting idea, not sure I love it though.

I also think in your example that Str.type would be a compiler error (since you are accessing an instance method as a static method).

Login or Signup to reply.