case class EarthMap(locationLL:WorldLocation=WorldLocation(lat= -90, lon= -180), // location on the globe in latitude;longitude
locationUR:WorldLocation=WorldLocation(lat=90, lon=179), // location on the globe in latitude;longitude
width:Int=360, height:Int=181, // size in pixels of the image file
borders:Boolean=true,
legend:Boolean=false,
grid:Option[(Double,Double)]=None,
projection:Projection = new SimpleProjection(locationLL.lon),
palette:ColorPalette = ColorPalette.defaultTemperatureColorPalette) {
...
I’d like to be able to reference an earlier initialization parameter from a later one.
Above is such an example, I’d like to use the call-site-provided locationLL
parameter from the projection
initialization value. This doesn’t work as is.
However, I accidentally discovered that I can create multiple parameter lists for the class definition, as follows, and it seems to work.
What I intend is that if the call-site does not specify locationLL
, then locationLL.lon
evaluates to -180.0. However, if the call-site specifies locationLL(lat=100.0,lon=45.0)
, then locationLL.lon
evaluates to 45.0
Is this a robust solution, or will I regret it later? Of course I have to go an update all my call-sites where I instantiate the class.
case class EarthMap(locationLL:WorldLocation=WorldLocation(lat= -90, lon= -180), // location on the globe in latitude;longitude
locationUR:WorldLocation=WorldLocation(lat= 90, lon=179), // location on the globe in latitude;longitude
width:Int=360, height:Int=181, // size in pixels of the image file
borders:Boolean=true,
legend:Boolean=false,
grid:Option[(Double,Double)]=None
)( // *** NOTE, providing 2nd parameter list
projection:Projection = new SimpleProjection(locationLL.lon),
palette:ColorPalette = ColorPalette.defaultTemperatureColorPalette) {
...