Skip to content
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

Is there any reason why pattern matching behavior different when processing generic method #1121

Closed
lust4life opened this issue Mar 6, 2020 · 1 comment

Comments

@lust4life
Copy link

@lust4life lust4life commented Mar 6, 2020

sealed trait DSL[A]{
  // def run(): A ={
  //   this match {
  //     case GetLength(something) =>
  //       something.length
  //     case ShowResult(number) =>
  //       s"the length is $number"
  //   }
  // }
}

case class GetLength(something: String) extends DSL[Int]
case class ShowResult(number: Int) extends DSL[String]

def run[A](fa:DSL[A]): A ={
  fa match {
    case GetLength(something) =>
      something.length
    case ShowResult(number) =>
      s"the length is $number"
  }
}

val dslGetLength = GetLength("123456789")
val length = run(dslGetLength)
val dslShowResult = ShowResult(length)
println(run(dslShowResult))
// print: the length is 9

From here , seems that pattern matching behavior different when processing generic method. Is there any reason to not do the same thing (the comments code) when in DSL[A] trait here ?

@SethTisue

This comment has been minimized.

Copy link
Member

@SethTisue SethTisue commented Mar 6, 2020

please use https://users.scala-lang.org or https://gitter.im/scala/scala or Stack Overflow for questions

@SethTisue SethTisue closed this Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.