The readonly keyword is essentially sugar for private set to make the setter private. However const fields don't have setters, therefore they can't make their setter private. So I pushed a fix to just report that that is an invalid combination of modifiers.
I think this is fine actually since in general only the class can set their own const fields. However the one case where it might crop up is if you wanted to expose a it-block constructor to allow setting some const field, but not others. But I think you can just handle that yourself by doing checks after you call the it-block, or using a little boiler plate with a getter method and private field.
andyMon 29 Mar 2010
I thought we were going to remove readonly keyword?
brianMon 29 Mar 2010
I thought we were going to remove readonly keyword?
We discussed it - can't remember all the details. But it turns out there is a lot of code that benefits from an easy way to make the setter private, so I think its a nice thing to keep in the language.
tacticsMon 29 Mar 2010
I could potentially see removing readonly if things swayed that way. Learning Fantom, I always got confused by the difference between const and readonly. (const means it can't change and readonly means you can't change it).
andyMon 29 Mar 2010
Learning Fantom, I always got confused by the difference between const and readonly
I sorta agree there - I think this code reads more clearly and avoids the ambiguity between const and readonly:
Int x { private set }
KevinKelleyMon 29 Mar 2010
Int x { private set }
Agree, that's what I do, I didn't realize we even had a readonly. :-)
katox Sat 27 Mar 2010
Test sample
Running
fan test.fan
yieldsbrian Sun 28 Mar 2010
The
readonly
keyword is essentially sugar forprivate set
to make the setter private. However const fields don't have setters, therefore they can't make their setter private. So I pushed a fix to just report that that is an invalid combination of modifiers.I think this is fine actually since in general only the class can set their own const fields. However the one case where it might crop up is if you wanted to expose a it-block constructor to allow setting some const field, but not others. But I think you can just handle that yourself by doing checks after you call the it-block, or using a little boiler plate with a getter method and private field.
andy Mon 29 Mar 2010
I thought we were going to remove
readonly
keyword?brian Mon 29 Mar 2010
We discussed it - can't remember all the details. But it turns out there is a lot of code that benefits from an easy way to make the setter private, so I think its a nice thing to keep in the language.
tactics Mon 29 Mar 2010
I could potentially see removing
readonly
if things swayed that way. Learning Fantom, I always got confused by the difference betweenconst
andreadonly
. (const
means it can't change andreadonly
means you can't change it).andy Mon 29 Mar 2010
I sorta agree there - I think this code reads more clearly and avoids the ambiguity between
const
andreadonly
:KevinKelley Mon 29 Mar 2010
Agree, that's what I do, I didn't realize we even had a
readonly
. :-)