In overriding sys::Bool.equals(Obj? o) I was trying something like the following:
if(o isnot MyClass)
return false
o = o as MyClass
return (o.myfield == this.myfield)
and was a little confused by the compiler errors I was getting, which were along the lines of "Unknown slot sys::Obj.myfield". If that cast is not possible/legal, I would have hoped to hear about it at the time of casting.
If I instead do something like
m := o as MyClass
return (m.myfield == this.myfield)
then everything is hunky dory. Is this just the way it is, or is a more precise compiler error possible?
tacticsWed 9 Dec 2009
The problem with the first example is the type on the variable o does not change. It starts its life as an Obj? and it ends its life as an Obj?. Obj? is the base type, and doesn't have a slot called myfield, thus, the error.
In the second case, you are defining a new variable called m. When you declare it, you are using type inference. So it looks at the RHS and sees the as MyClass and types the whole expression as a MyClass?.
There are other ways that work in Fantom. Since you know that o is a MyClass after the if statement, you could also use dynamic typing:
if (o isnot MyClass)
return false
return o->myfield == this.myfield
You could also do a cast, like you would in Java:
if (o isnot MyClass)
return false
return ((MyClass)o).myfield == this.myfield
Or use the as keyword to do the cast:
if (o isnot MyClass)
return false
return (o as MyClass).myfield == this.myfield
liamstaskWed 9 Dec 2009
Thanks :) I understood why it didn't work, but was curious mostly about the compiler error. Ideally it would tell me that I can't reassign an object to a casted version of itself right when I try to do that. Unless that's not possible/reasonable in this case, somehow?
Not a huge deal, just a potential clarification.
heliumWed 9 Dec 2009
Ideally it would tell me that I can't reassign an object to a casted version of itself right when I try to do that.
Of course you can. That's why the compiler didn't complain. But that doesn't change the static type.
liamstaskWed 9 Dec 2009
Just because compiler didn't complain doesn't mean it couldn't have been a problem :)
Are there cases in which assigning a casted version of an object to itself would be useful? I'm curious about why that is possible.
tacticsWed 9 Dec 2009
The compiler's job isn't to tell you when you've done something pointless ;-)
Trying to determine whether or not a statement in redundant is an insoluble problem. You can apply heuristics in the compiler to catch special cases, but you can't prevent it entirely. Some languages, for example, prevent self-assignment:
x := 3
x = x
But usually, these kinds of errors are: 1) harmless, 2) easy to spot, and it isn't really worth the effort to gunk up the compiler with checks.
heliumWed 9 Dec 2009
I'm curious about why that is possible.
Because it's logical and consistent even if it's pointless. The expression has a type compatible with the type of the variable it is assigned to, so it should compile. Otherwise I would be surprised.
The compiler could warn you about pointless code but it should IMO not be an error. If you have a lot of time you can add lots of tiny special rules that warn about things like x + 0 or whatever but perhaps you should spend your time on something more productive.
brianThu 10 Dec 2009
Some languages, for example, prevent self-assignmen
BTW Fantom does check self assignment (since we have both implicit this and it):
fansh> x := 3
3
fansh> x = x
ERROR(1): Self assignment
liamstaskThu 10 Dec 2009
Thanks for the info - nice to have a little more context with which to work.
freddy33Thu 10 Dec 2009
BTW: finding pointless, useless, unreachable or potentially dangerous code is the job of IDE inspections and tools like FindBugs.
liamstask Wed 9 Dec 2009
In overriding
sys::Bool.equals(Obj? o)
I was trying something like the following:and was a little confused by the compiler errors I was getting, which were along the lines of "Unknown slot
sys::Obj.myfield
". If that cast is not possible/legal, I would have hoped to hear about it at the time of casting.If I instead do something like
then everything is hunky dory. Is this just the way it is, or is a more precise compiler error possible?
tactics Wed 9 Dec 2009
The problem with the first example is the type on the variable
o
does not change. It starts its life as anObj?
and it ends its life as anObj?
.Obj?
is the base type, and doesn't have a slot calledmyfield
, thus, the error.In the second case, you are defining a new variable called
m
. When you declare it, you are using type inference. So it looks at the RHS and sees theas MyClass
and types the whole expression as aMyClass?
.There are other ways that work in Fantom. Since you know that o is a
MyClass
after theif
statement, you could also use dynamic typing:You could also do a cast, like you would in Java:
Or use the
as
keyword to do the cast:liamstask Wed 9 Dec 2009
Thanks :) I understood why it didn't work, but was curious mostly about the compiler error. Ideally it would tell me that I can't reassign an object to a casted version of itself right when I try to do that. Unless that's not possible/reasonable in this case, somehow?
Not a huge deal, just a potential clarification.
helium Wed 9 Dec 2009
Of course you can. That's why the compiler didn't complain. But that doesn't change the static type.
liamstask Wed 9 Dec 2009
Just because compiler didn't complain doesn't mean it couldn't have been a problem :)
Are there cases in which assigning a casted version of an object to itself would be useful? I'm curious about why that is possible.
tactics Wed 9 Dec 2009
The compiler's job isn't to tell you when you've done something pointless ;-)
Trying to determine whether or not a statement in redundant is an insoluble problem. You can apply heuristics in the compiler to catch special cases, but you can't prevent it entirely. Some languages, for example, prevent self-assignment:
But usually, these kinds of errors are: 1) harmless, 2) easy to spot, and it isn't really worth the effort to gunk up the compiler with checks.
helium Wed 9 Dec 2009
Because it's logical and consistent even if it's pointless. The expression has a type compatible with the type of the variable it is assigned to, so it should compile. Otherwise I would be surprised.
The compiler could warn you about pointless code but it should IMO not be an error. If you have a lot of time you can add lots of tiny special rules that warn about things like
x + 0
or whatever but perhaps you should spend your time on something more productive.brian Thu 10 Dec 2009
BTW Fantom does check self assignment (since we have both implicit this and it):
liamstask Thu 10 Dec 2009
Thanks for the info - nice to have a little more context with which to work.
freddy33 Thu 10 Dec 2009
BTW: finding pointless, useless, unreachable or potentially dangerous code is the job of IDE inspections and tools like FindBugs.
It should never be the work of the compiler!