How to define a constant members in Scala?
What is the correct was to define a constant such as Pi or the Golden Ratio in a Scala program?
As an example, in C# I can do this:
class Example { public readonly static Double GoldenRatio; static Example () { GoldenRatio = (1.0 + Math.Sqrt (5.0)) / 2.0; } }
11 Answer
It would be just a val
member:
object Example { val GoldenRatio = (1.0 + Math.sqrt(5.0)) / 2.0 }
Also, take a look at the Scala Style Guide section regarding constants.
6ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaW1wYmuFcXyOoaawZaSkeqWxxaKlnmWRYrCwutKtmKesXaKyrq7Eq6pmoZ5iwKSty5o%3D
Tisa Delillo
Update: 2024-06-24