我在一個trait中定義了一個方法:
我在一個trait中定義了一個方法。
trait myTrait {
def myMethod(物體。Entity = null, name: String)
}
val t = ....//myTrait的具體實作。
t.myMethod("John")
我在方法定義中為物體提供了默認值null。
這就產生了編譯錯誤,因為。Type mismatch, required MyCustomClass found String
為什么會這樣?
為什么會這樣呢?
uj5u.com熱心網友回復:
使用你的引數的名稱。
t.myMethod(name = "John")
當你想.copy
case class MyStruct(a。String, b: String)
val a = MyStruct("foo"/span>, "bar"/span>)
val b = a.copy(b = "lazz")
參見https://docs.scala-lang.org/tour/named-arguments.html
另外,在scala中,值的存在/不存在通常由Option[T]表示,如
def myMethod(物體。Option[Entity] = None, ...。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/309126.html
標籤:
上一篇:有沒有一種Scala模式來收集一連串獨立計算的錯誤資訊?
下一篇:Flink廣播狀態
