Skip to content

Problem with using a ContextFunction1 for +B in PartialFunction[-A, +B]. #15741

Closed
@takapi327

Description

@takapi327

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 =>.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions