Closed
Description
Compiler version
3.1.2
Description
If you use PartialFunction with ? =>, if you use apply, you will get a Match Error if you pass a key that does not exist.
However, if isDefinedAt or lift is used, it is possible to get the value even if a non-existent key is passed.
Minimized code
use Int => String
@main def Main: Unit =
def pf1: PartialFunction[String, Int => String] = {
case "hoge" => int => int.toString
case "huga" => int => int.toString
}
println(pf1.isDefinedAt("hoge"))
println(pf1.isDefinedAt("huga"))
println(pf1("huga")(1))
println(pf1.unapply("hogehoge"))
println(pf1.isDefinedAt("hogehoge"))
use Int ?=> String
@main def Main: Unit =
def get(using Int): String = summon[Int].toString
def pf2: PartialFunction[String, Int ?=> String] = {
case "hoge" => get
case "huga" => get
}
println(pf2.isDefinedAt("hoge"))
println(pf2.isDefinedAt("huga"))
println(pf2("huga")(using 2))
println(pf2.unapply("hogehoge"))
println(pf2.isDefinedAt("hogehoge"))
Output
use Int => String
[info] true
[info] true
[info] 1
[info] None
[info] false
use Int ?=> String
[info] true
[info] true
[info] 2
[info] Some(Main$package$$anon$2$$Lambda$7/0x0000000800bf0c80@3fb6a447)
[info] true
Expectation
The results with ?=> are similar to those with =>.