If I define this Fantom class
const class Mixed
{
const Int whole
const Int numerator
const Int denominator
const | -> Int[]| convertToFrac
new make( |This| func ) { func( this ) }
}
And I want to create an instance defining the convertToFrac function, like this:
class Example
{
Void main( Str args )
{
mixed := Mixed {
whole = 2
numerator = 3
denominator = 8
convertToFrac = |->Int[]| {
return [ whole * denominator + numerator, denominator ]
}
}
}
}
The compiler complains saying:
Unknown variable 'numerator'Unknown variable 'denominator'Unknown variable 'whole'
Is there any way to refer to the object "mixed" being created from within the function "convertToFrac", also being defined, without passing the "mixed" object as a parameter of the function?
If I prepend each variable with "mixed", like so:
return [ mixed.whole * mixed.denominator + mixed.numerator, mixed.denominator ]
The compiler complains: Unknown variable 'mixed'.
Using this.whole doesn't make sense as it refers to the Example class.
Using it.whole doesn't make sense either as it refers to the Function.
Can anyone please suggest the way to access the "mixed" object from within the "convertToFrac" function?
As you correctly assessed, the issue is that you're using an
it-blockinside anit-block, and because you're using an implicitit(i.e. you're don't have anyitqualifiers) there is confusion as to what's being referenced.I'll write out the
itqualifiers out long hand so you can see what's going on:Your idea of using the
mixedvariable qualifier was a good one but, unfortunately, whilst processing the ctor themixedvariable hasn't been created yet so can't be referenced.But you can create your own
mixedvariable in the it-block, and the following compiles and runs quite happily: