【发布时间】:2021-08-31 12:42:58
【问题描述】:
我使用外部库 plotly-scala,这是一个实现 Layout object 的图形包。
我需要使用shape 参数,这似乎需要详细的方法签名,我想了解原因以及是否有任何解决方法。
Shape 对象定义为here。知道 scala-plotly 类依赖同一作者的 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's 乱扔代码吗?
完整的错误信息:
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
【问题讨论】:
-
@deprecated("改用 Layout() 和链式调用 .with* 方法", "0.8.0")
-
“相关问题:为什么我必须在任何地方都使用 Some(...)?” 因为构造函数需要
Options,所以你不能只通过普通的值,您需要将其包装成Option,这只是 Scala 101,在尝试使用第三方库之前学习一点语言会很好。 -
@LuisMiguelMejíaSuárez 我知道如果该方法需要
Option[A]类型的参数,我需要返回 Some[A] 或 None,但为什么Layout不是这样(没有形状参数),其源代码中的签名还包括 Option[A] 类型? -
我的意思是,答案可能是因为库的作者决定允许两个签名,一个带有一组受限的参数但没有 Option[A] 要求,另一个带有除 Option[A] 之外的所有参数的签名] 类型。无论如何,这就是错误消息所暗示的内容,我可以忍受它,但我想确保我没有错过标准库中允许我跳过选项要求的功能。
-
感谢“Layout() 和链式调用 .with*”的提示,我只需要了解它的含义(也许是
Layout().with(title="simulation").with(shape=etc.)?)
标签: scala plotly data-class