Scala "overloaded method apply with alternatives" - 理解方法签名
Scala "overloaded method apply with alternatives" - understand method signatures
我使用外部库 plotly-scala,这是一个实现 Layout
object.
的图形包
我需要使用 shape
参数,这似乎需要一个冗长的方法签名,我想了解原因以及是否有任何解决方法。
形状对象由同一作者here. It may or may not help to know that that scala-plotly classes rely on a 3rd party library定义。
我定义了工作和非工作示例:
// Related question: why do I have to use Some(...) everywhere?
// I'd prefer to write: new Shape(`type`="rect",xref="x", etc.)
val confidenceBand = new Shape(
`type`=Some("rect"),
xref=Some("x"),
yref=Some("paper"),
x0=Some("2.1"),
y0=Some(0.0),
x1=Some("3.2"),
y1=Some(1.0),
fillcolor=Some(Color.RGBA(156, 165, 196, 1.0)),
opacity=Some(0.8),
line=Some(Line(color = Color.RGBA(156, 165, 196, 1.0), width = 1.0)),
)
// No shape parameter -> non verbose signature, no need to use Some/None
val layout = Layout(
title="Simulation",
yaxis = Axis(
title = "tbc")
)
// With shape parameter -> `overloaded method apply with alternatives:`
// See full error further down
val layout = Layout(
title="Simulation",
yaxis = Axis(
title = "y"),
shapes=Some(Seq(confidenceBand))
)
// With shape parameter and a verbose signature -> code compiles
val layout = Layout(
title=Some("Simulation"),
legend=None,
width=None,
height=None,
showlegend=None,
xaxis=None,
yaxis = Some(Axis(
title = "y")),
shapes=Some(Seq(confidenceBand)),
xaxis1=None,
xaxis2=None,
xaxis3=None,
xaxis4=None,
yaxis1=None,
yaxis2=None,
yaxis3=None,
yaxis4=None,
barmode=None,
autosize=None,
margin=None,
annotations=None,
plot_bgcolor=None,
paper_bgcolor=None,
font=None,
bargap=None,
bargroupgap=None,
hovermode=None,
boxmode=None,
scene=None,
dragmode=None,
)
我能否采用第二种方法来工作,即具有形状参数但不必在代码中乱扔 None
?
完整的错误信息:
cmd66.sc:1: overloaded method apply with alternatives:
(title: Option[String],legend: Option[plotly.layout.Legend],width: Option[Int],height: Option[Int],showlegend: Option[scala.Boolean],xaxis: Option[plotly.layout.Axis],yaxis: Option[plotly.layout.Axis],xaxis1: Option[plotly.layout.Axis],xaxis2: Option[plotly.layout.Axis],xaxis3: Option[plotly.layout.Axis],xaxis4: Option[plotly.layout.Axis],yaxis1: Option[plotly.layout.Axis],yaxis2: Option[plotly.layout.Axis],yaxis3: Option[plotly.layout.Axis],yaxis4: Option[plotly.layout.Axis],barmode: Option[plotly.layout.BarMode],autosize: Option[scala.Boolean],margin: Option[plotly.layout.Margin],annotations: Option[Seq[plotly.layout.Annotation]],plot_bgcolor: Option[plotly.element.Color],paper_bgcolor: Option[plotly.element.Color],font: Option[plotly.layout.Font],bargap: Option[scala.Double],bargroupgap: Option[scala.Double],hovermode: Option[plotly.layout.HoverMode],boxmode: Option[plotly.layout.BoxMode],scene: Option[plotly.layout.Scene],dragmode: Option[String],shapes: Option[Seq[plotly.layout.Shape]])plotly.layout.Layout <and>
(title: Option[String],legend: Option[plotly.layout.Legend],width: Option[Int],height: Option[Int],showlegend: Option[scala.Boolean],xaxis: Option[plotly.layout.Axis],yaxis: Option[plotly.layout.Axis],xaxis1: Option[plotly.layout.Axis],xaxis2: Option[plotly.layout.Axis],xaxis3: Option[plotly.layout.Axis],xaxis4: Option[plotly.layout.Axis],yaxis1: Option[plotly.layout.Axis],yaxis2: Option[plotly.layout.Axis],yaxis3: Option[plotly.layout.Axis],yaxis4: Option[plotly.layout.Axis],barmode: Option[plotly.layout.BarMode],autosize: Option[scala.Boolean],margin: Option[plotly.layout.Margin],annotations: Option[Seq[plotly.layout.Annotation]],plot_bgcolor: Option[plotly.element.Color],paper_bgcolor: Option[plotly.element.Color],font: Option[plotly.layout.Font],bargap: Option[scala.Double],bargroupgap: Option[scala.Double],hovermode: Option[plotly.layout.HoverMode],boxmode: Option[plotly.layout.BoxMode],scene: Option[plotly.layout.Scene])plotly.layout.Layout <and>
()plotly.layout.Layout <and>
(title: String,legend: plotly.layout.Legend,width: Integer,height: Integer,showlegend: java.lang.Boolean,xaxis: plotly.layout.Axis,yaxis: plotly.layout.Axis,xaxis1: plotly.layout.Axis,xaxis2: plotly.layout.Axis,xaxis3: plotly.layout.Axis,xaxis4: plotly.layout.Axis,yaxis1: plotly.layout.Axis,yaxis2: plotly.layout.Axis,yaxis3: plotly.layout.Axis,yaxis4: plotly.layout.Axis,barmode: plotly.layout.BarMode,autosize: java.lang.Boolean,margin: plotly.layout.Margin,annotations: Seq[plotly.layout.Annotation],plot_bgcolor: plotly.element.Color,paper_bgcolor: plotly.element.Color,font: plotly.layout.Font,bargap: java.lang.Double,bargroupgap: java.lang.Double,hovermode: plotly.layout.HoverMode,boxmode: plotly.layout.BoxMode,scene: plotly.layout.Scene)plotly.layout.Layout
cannot be applied to (title: String, yaxis: plotly.layout.Axis, shapes: Some[Seq[plotly.layout.Shape]])
val layout = Layout(
^
Compilation Failed
正如@LuisMiguelMejíaSuárez 在评论中明确解释的那样,答案在 Layout
的伴随对象中,它根据我们传递的 A 类型参数创建 Option[A] 参数。
apply 方法不包含 shapes
参数,因此出现了我描述的奇怪行为。
此外,使用链接方法 - class 构造函数中已弃用的警告建议 - 给了我我需要的东西:
val layout = Layout().
withTitle("simulation").
withYaxis(Axis(title = "y")).
withShapes(Seq(confidenceBand))
我使用外部库 plotly-scala,这是一个实现 Layout
object.
我需要使用 shape
参数,这似乎需要一个冗长的方法签名,我想了解原因以及是否有任何解决方法。
形状对象由同一作者here. It may or may not help to know that that scala-plotly classes rely on a 3rd party library定义。
我定义了工作和非工作示例:
// Related question: why do I have to use Some(...) everywhere?
// I'd prefer to write: new Shape(`type`="rect",xref="x", etc.)
val confidenceBand = new Shape(
`type`=Some("rect"),
xref=Some("x"),
yref=Some("paper"),
x0=Some("2.1"),
y0=Some(0.0),
x1=Some("3.2"),
y1=Some(1.0),
fillcolor=Some(Color.RGBA(156, 165, 196, 1.0)),
opacity=Some(0.8),
line=Some(Line(color = Color.RGBA(156, 165, 196, 1.0), width = 1.0)),
)
// No shape parameter -> non verbose signature, no need to use Some/None
val layout = Layout(
title="Simulation",
yaxis = Axis(
title = "tbc")
)
// With shape parameter -> `overloaded method apply with alternatives:`
// See full error further down
val layout = Layout(
title="Simulation",
yaxis = Axis(
title = "y"),
shapes=Some(Seq(confidenceBand))
)
// With shape parameter and a verbose signature -> code compiles
val layout = Layout(
title=Some("Simulation"),
legend=None,
width=None,
height=None,
showlegend=None,
xaxis=None,
yaxis = Some(Axis(
title = "y")),
shapes=Some(Seq(confidenceBand)),
xaxis1=None,
xaxis2=None,
xaxis3=None,
xaxis4=None,
yaxis1=None,
yaxis2=None,
yaxis3=None,
yaxis4=None,
barmode=None,
autosize=None,
margin=None,
annotations=None,
plot_bgcolor=None,
paper_bgcolor=None,
font=None,
bargap=None,
bargroupgap=None,
hovermode=None,
boxmode=None,
scene=None,
dragmode=None,
)
我能否采用第二种方法来工作,即具有形状参数但不必在代码中乱扔 None
?
完整的错误信息:
cmd66.sc:1: overloaded method apply with alternatives:
(title: Option[String],legend: Option[plotly.layout.Legend],width: Option[Int],height: Option[Int],showlegend: Option[scala.Boolean],xaxis: Option[plotly.layout.Axis],yaxis: Option[plotly.layout.Axis],xaxis1: Option[plotly.layout.Axis],xaxis2: Option[plotly.layout.Axis],xaxis3: Option[plotly.layout.Axis],xaxis4: Option[plotly.layout.Axis],yaxis1: Option[plotly.layout.Axis],yaxis2: Option[plotly.layout.Axis],yaxis3: Option[plotly.layout.Axis],yaxis4: Option[plotly.layout.Axis],barmode: Option[plotly.layout.BarMode],autosize: Option[scala.Boolean],margin: Option[plotly.layout.Margin],annotations: Option[Seq[plotly.layout.Annotation]],plot_bgcolor: Option[plotly.element.Color],paper_bgcolor: Option[plotly.element.Color],font: Option[plotly.layout.Font],bargap: Option[scala.Double],bargroupgap: Option[scala.Double],hovermode: Option[plotly.layout.HoverMode],boxmode: Option[plotly.layout.BoxMode],scene: Option[plotly.layout.Scene],dragmode: Option[String],shapes: Option[Seq[plotly.layout.Shape]])plotly.layout.Layout <and>
(title: Option[String],legend: Option[plotly.layout.Legend],width: Option[Int],height: Option[Int],showlegend: Option[scala.Boolean],xaxis: Option[plotly.layout.Axis],yaxis: Option[plotly.layout.Axis],xaxis1: Option[plotly.layout.Axis],xaxis2: Option[plotly.layout.Axis],xaxis3: Option[plotly.layout.Axis],xaxis4: Option[plotly.layout.Axis],yaxis1: Option[plotly.layout.Axis],yaxis2: Option[plotly.layout.Axis],yaxis3: Option[plotly.layout.Axis],yaxis4: Option[plotly.layout.Axis],barmode: Option[plotly.layout.BarMode],autosize: Option[scala.Boolean],margin: Option[plotly.layout.Margin],annotations: Option[Seq[plotly.layout.Annotation]],plot_bgcolor: Option[plotly.element.Color],paper_bgcolor: Option[plotly.element.Color],font: Option[plotly.layout.Font],bargap: Option[scala.Double],bargroupgap: Option[scala.Double],hovermode: Option[plotly.layout.HoverMode],boxmode: Option[plotly.layout.BoxMode],scene: Option[plotly.layout.Scene])plotly.layout.Layout <and>
()plotly.layout.Layout <and>
(title: String,legend: plotly.layout.Legend,width: Integer,height: Integer,showlegend: java.lang.Boolean,xaxis: plotly.layout.Axis,yaxis: plotly.layout.Axis,xaxis1: plotly.layout.Axis,xaxis2: plotly.layout.Axis,xaxis3: plotly.layout.Axis,xaxis4: plotly.layout.Axis,yaxis1: plotly.layout.Axis,yaxis2: plotly.layout.Axis,yaxis3: plotly.layout.Axis,yaxis4: plotly.layout.Axis,barmode: plotly.layout.BarMode,autosize: java.lang.Boolean,margin: plotly.layout.Margin,annotations: Seq[plotly.layout.Annotation],plot_bgcolor: plotly.element.Color,paper_bgcolor: plotly.element.Color,font: plotly.layout.Font,bargap: java.lang.Double,bargroupgap: java.lang.Double,hovermode: plotly.layout.HoverMode,boxmode: plotly.layout.BoxMode,scene: plotly.layout.Scene)plotly.layout.Layout
cannot be applied to (title: String, yaxis: plotly.layout.Axis, shapes: Some[Seq[plotly.layout.Shape]])
val layout = Layout(
^
Compilation Failed
正如@LuisMiguelMejíaSuárez 在评论中明确解释的那样,答案在 Layout
的伴随对象中,它根据我们传递的 A 类型参数创建 Option[A] 参数。
apply 方法不包含 shapes
参数,因此出现了我描述的奇怪行为。
此外,使用链接方法 - class 构造函数中已弃用的警告建议 - 给了我我需要的东西:
val layout = Layout().
withTitle("simulation").
withYaxis(Axis(title = "y")).
withShapes(Seq(confidenceBand))