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
class X { readonly const Int x := 2 }Running
fan test.fanyieldsbrian Sun 28 Mar 2010
The
readonlykeyword is essentially sugar forprivate setto 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
readonlykeyword?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
readonlyif things swayed that way. Learning Fantom, I always got confused by the difference betweenconstandreadonly. (constmeans it can't change andreadonlymeans 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
constandreadonly:Int x { private set }KevinKelley Mon 29 Mar 2010
Int x { private set }Agree, that's what I do, I didn't realize we even had a
readonly. :-)