score:1
The shortest, simplest way.
Define a case class:
case class Options(
option1: Int,
option2: String
/* ... */
) extends Opts
and implicit conversion from Opts to your Options
object OptsConverter {
implicit def toOptions(opts: Opts) = Options(
option1 = opts.option1,
option2 = opts.option2
/* ... */
)
}
That way you get all copy methods (generated by compiler) for free. You can use it like that:
import OptsConverter.toOptions
def usage(opts: Opts) = {
val improvedOpts = opts.copy(option2 = "improved")
/* ... */
}
Note, that Options extends Opts, so you can use it whenever Opts is required. You'll be able to call copy to obtain a modified instance of Opts in every place where you import the implicit conversion.
score:1
The simplest solution is to allow the trait to define it's own "copy" method, and allow it's subclasses (or even base class) to work with that. However, the parameters can really only match the base class unless you recast it later. Incidentally this doesn't work as "mixed in" so your root might as well be an abstract class, but it works the same way. The point of this is that the subclass type keeps getting passed along as it's copied.
(Sorry I typed this without a compiler so it may need some work)
trait A {
type myType<:A
def option1: Int
def option2: String
def copyA(option1_:Int=option1, option2_String=option2):myType = new A {
def option1 = option_1
def option2 = option_2
}
}
trait B extends A { me=>
type myType = B
def option3: Double
//callable from A but properly returns B
override def copyA(option1_:Int=option1, option2_:String=option2):myType = new B {
def option1 = option_1
def option2 = option_2
def option3 = me.option3
}
//this is only callable if you've cast to type B
def copyB(option1_:Int=option1, option2_:String=option2, option3_:Double=option3):myType = new B {
def option1 = option_1
def option2 = option_2
def option3 = option_3
}
}
score:2
I would first check if using the Opts trait (solely) is an absolute necessity. Hopefully it's not and then you just extend the trait, overriding defs with vars, like you said.
When Opts is mandatory and you have its instance that you want to copy modifying some fields, here's what you could do:
Write a wrapper for Opts, which extends Opts, but delegates every call to the wrapped Opts excluding the fields that you want to modify. Set those fields to values you want. Writing the wrapper for a broad-interface trait can be boring task, therefore you may consider using http://www.warski.org/blog/2013/09/automatic-generation-of-delegate-methods-with-macro-annotations/ to let macros generate most of it automatically.
Source: stackoverflow.com
Related Query
- Modifying value returned from a method in Scala (Highcharts lib)
- Highcharts - How to start x axis from an arbitrary value
- Why won't Highcharts accept UTC value from my JSON?
- Using PhantomJS to create HighCharts grahps server side for use in PDF creation (PHP) - results in exit code 11 from PHPs exec();
- Highcharts - set yAxis.max to max value from data
- How to remove the value and number labels from Highcharts angular gauge
- How to suppress items with NULL value from HighCharts legend
- passing json values to highcharts from .net code behind
- Getting the corresponding x-axis value from the max value in y-axis in Highcharts
- Use value from prompt as highcharts export file name
- Remove gap in Highcharts column chart from null value from JSON file in React
- Tags disappeared from tagcloud in highcharts while using deriveFontSize method
- Highcharts set xaxis value from array
- highcharts with more series that start from different x value
- Passing Returned Data from a PHP Script into a Highcharts data[] block?
- Highcharts SVG Export from Python Server Side Code
- Loop in xAxis from highcharts according to returned data
- Highcharts plot band to last datetime value from spreadsheet data
- Highcharts PlotLines using specific value from JS array
- Getting value from tooltip when hover on svg element on the graph created with highcharts
- Can't access custom value from point HighCharts
- Getting numbers on Y-axis to show up as percentages with code from a Highcharts code generator tool?
- Highcharts - Get x Value From Y value
- HighCharts not plotting data coming from model method in rails
- Highcharts / Highstock - toggle yaxis from compare to value and back to compare
- The Gauge needle is always started from 0 when it's value is updated | HighCharts with Angular 7
- highcharts with dynamically adding multiple series from JavaScript array name value pair
- Highcharts data value from variable array string
- Laravel Highcharts - Cant set value and labels from arrays
- How to get time and value from MySQL into Highcharts
More Query from same tag
- Draggable crosshair in Highcharts
- change parameter with {{index+1}} angularjs
- highcharts tooltip z-index
- Use of highcharts' addChart function within a getJSON call (looping over multiple local files)
- Highcharts Bar Chart Without Column
- How to add highcharts plugins into highcharts-vue(highcharts vue wrapper)
- How to enable dates that match with the data in rangeselector?
- Highstock - Enable marker only on broken line
- How to return Highcharts in Apollo Query component?
- How to set individual colors for high chart pies
- Highcharts add&remove data to series in stacked-column chart
- How to reformat a HighChart series to for CSV export
- Highcharts plotBand animation
- HighCharts: Display total value below the legend title
- Legend name and color need to be displayed in the export of highchart as image
- limit number of xAxis in a highchart
- Custom YTD Option in HighStock Range Selector
- Combine multiple Yaxis in one
- Highcharts force to 0 before climbing again
- how to make different label for different arrows in Gauge chart in highchart
- Fixed padding between the bars in Highcharts
- How to add custom fields to existing highcharts annotation
- Create a heatmap using Yii2 highcharts widget
- Realtime monitoring node.js server
- ASP.NET MVC Angular 2 Failed to load resource: the server responded with a status of 404 (Not Found)
- Highcharts timeline series positioning
- append the min max value lableof solidguage with string in highchart
- logarithmic chart with 0 values
- Image not exporting correctly in highcharts
- How to call Objective-C function from javascript in iOS