I've just got started learning Fantom, and to get familiar with the language I'm working through and adding examples at Rosetta Code: http://rosettacode.org/wiki/Category:Fantom
If you take a look you'll find mostly the solutions are straightforward, but do let me know if there are any better Fantom idioms to be learnt. Comments on the Accumulator Factory problem in particular would be appreciated - my approach seems too much like what the language should be doing for me!?
The difficulty is with variables which could be one or other of the Num types. Changing the precise numeric type of a variable does not seem to be a problem:
fansh> Num x := 0.2f
0.2
fansh> x.typeof
sys::Float
fansh> x = 2
fansh> x.typeof
sys::Int
But calling a method on that variable is not so easy:
fansh> x + 2
ERROR(5): No operator method found: sys::Num? + sys::Int
fansh> x.plus(4)
ERROR(7): Unknown method 'sys::Num.plus'
fansh> x->plus(4)
8
fansh> x->plus(4.1)
sys::ArgErr: java.lang.IllegalArgumentException: argument type mismatch
In contrast, Ruby makes the following possible:
irb> x = 2
irb> x.class
Fixnum
irb> x = x + 4
=> 6
irb> x.class
=> Fixnum
irb> x = x + 0.1
=> 6.1
irb> x.class
=> Float
Just curious really if the Ruby-like way is at all possible using some dynamic feature of Fantom, or if the type system means it's impossible (without writing the 9-way switch I did for the Rosetta Code problem).
qualidafialSat 29 Jan 2011
In the 100 doors example, use 1..100 instead of Range.makeInclusive(1, 100).
qualidafialSat 29 Jan 2011
As for your addition problem, you have a reference of type Num which has no arithmetic operators defined. They are all defined in the concrete classes Int, Num and Decimal.
Brian, Andy: could we pull these up to Num? They would have to be abstract and would have to return type Num, but those return types could be overridden in each concrete class. Also, the simply named methods e.g. Int.plus(Int), Float.plus(Float) would have to be renamed to plusInt and plusFloat respectively so that the method names are the same between all three types.
DanielFathSat 29 Jan 2011
Yeah, peter you are right, there isn't a way for Num to be added to Float. However Int can be used instead of Num.
peter Sat 29 Jan 2011
I've just got started learning Fantom, and to get familiar with the language I'm working through and adding examples at Rosetta Code: http://rosettacode.org/wiki/Category:Fantom
If you take a look you'll find mostly the solutions are straightforward, but do let me know if there are any better Fantom idioms to be learnt. Comments on the Accumulator Factory problem in particular would be appreciated - my approach seems too much like what the language should be doing for me!?
The difficulty is with variables which could be one or other of the Num types. Changing the precise numeric type of a variable does not seem to be a problem:
But calling a method on that variable is not so easy:
In contrast, Ruby makes the following possible:
Just curious really if the Ruby-like way is at all possible using some dynamic feature of Fantom, or if the type system means it's impossible (without writing the 9-way switch I did for the Rosetta Code problem).
qualidafial Sat 29 Jan 2011
In the 100 doors example, use
1..100
instead ofRange.makeInclusive(1, 100)
.qualidafial Sat 29 Jan 2011
As for your addition problem, you have a reference of type
Num
which has no arithmetic operators defined. They are all defined in the concrete classesInt
,Num
andDecimal
.Brian, Andy: could we pull these up to
Num
? They would have to be abstract and would have to return typeNum
, but those return types could be overridden in each concrete class. Also, the simply named methods e.g.Int.plus(Int)
,Float.plus(Float)
would have to be renamed toplusInt
andplusFloat
respectively so that the method names are the same between all three types.DanielFath Sat 29 Jan 2011
Yeah, peter you are right, there isn't a way for
Num
to be added to Float. HoweverInt
can be used instead ofNum
.As for your example. I'm not sure why adding
Decimal
onInt
doesn't work (maybe it's the size mismatch). Change your example fromto
peter Sat 29 Jan 2011
Thanks qualidafial, that's a nice syntax for defining ranges:
1..100
to include the end, and1..<100
to exclude the end.