Skip to content

Inlined mapping function fails when an inlined match is used #14325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hmf opened this issue Jan 22, 2022 · 4 comments
Open

Inlined mapping function fails when an inlined match is used #14325

hmf opened this issue Jan 22, 2022 · 4 comments

Comments

@hmf
Copy link

hmf commented Jan 22, 2022

Compiler version

3.1.0

Minimized code

val t1          = ( 1,   2L,  3.0f,   4.0,    "five",    '6'   )
val csvStandard = ("1", "2", "3.0",  "4.0", "\"five\"",  "6"   )

inline def toCSV: [t] => t => String = [t] => (x: t) => x match {
  case x: String => s"\"$x\""
  case x:_ => x.toString
}

val t1o3 = t1.map[[t] =>> String](toCSV)
println(t1o3)

works and prints

(1,2,3.0,4.0,"five",6)

But this does not (inline match):

val t1          = ( 1,   2L,  3.0f,   4.0,    "five",    '6'   )
val csvStandard = ("1", "2", "3.0",  "4.0", "\"five\"",  "6"   )

inline def toCSV: [t] => t => String = [t] => (x: t) => x match {
  case x: String => s"\"$x\""
  case x:_ => x.toString
}

val t1o3 = t1.map[[t] =>> String](toCSV)
println(t1o3)

Output

In the case above I get:

(1,2,3.0,4.0,five,6)

It seems like no type information is available for use. The default case is used.

Expectation

I was expecting to have the same output. If this is to be expected then I have seen several open issues regarding matching tuples inline, but cannot say if this is the same issue.

Note: I tried to create scastie example but failed due to a compilation error on the latest and RC versions.

@hmf hmf added the itype:bug label Jan 22, 2022
@Adam-Vandervorst
Copy link

This doesn't even work on a single element; see #13774

def toCSV: [t] => t => String = [t] => (x: t) => x match
  case x: String => s"\"$x\""
  case x:_ => x.toString

inline def toCSVinlined: [t] => t => String = [t] => (x: t) => inline x match
  case x: String => s"\"$x\""
  case x:_ => x.toString


@main def m =
  println(toCSV("foo"))
  println(toCSVinlined("foo"))

@hmf
Copy link
Author

hmf commented Jan 24, 2022

@Adam-Vandervorst You are correct. Should have checked that before reporting. I will edit the title accordingly. Thank you.

However, assuming no tuples are used, this case is not quite the same as the one you referenced. In that case we had an Int and matched against 1. Here we have a generic type t and match against a more specific type String (could also be an Int, etc.). So this should work, right?

TIA

@hmf hmf changed the title Tuple map fails when inlined mapping function also has an inlined match Inlined mapping function fails when an inlined match is used Jan 24, 2022
@nicolasstucki
Copy link
Contributor

def toCSV: [t] => t => String = [t] => (x: t) => x match
  case x: String => s"\"$x\""
  case x => "other"

inline def toCSVinlined: [t] => t => String = [t] => (x: t) => inline x match
  case x: String => s"\"$x\""
  case x => "other"

inline def toCSVinlined2[T](x: T): String = inline x match
  case x: String => s"\"$x\""
  case x => "other"

@main def test =
  println(toCSV("foo")) // "foo"
  println(toCSVinlined("foo")) // other
  println(toCSVinlined2("foo")) // "foo"

the inline match of toCSVinlined is choosing the second branch because in that case x is statically known to be a t and not a String. This is correct, but we might need to disallow this pattern because they are overlapping, a String could match any of the branches.

@hmf
Copy link
Author

hmf commented Jan 27, 2022

I confess I am confused. Aren't toCSVinlined and toCSVinlined2 "equivalent" (function and method respectively)? Don't t and T represent the same type parameter? Any pointers or examples that can elucidate the differences are much appreciated. Documentation did not help me :-(.

TIA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants