These are chat archives for typelevel/scala

11th
Apr 2017
Abhishek Jain
@jaina00
Apr 11 2017 20:14

Hi.. I am having an issue with pattern matching a list of sub types.

object Test extends App{

  type Error = String

  sealed trait ParentType

  case class Child1() extends ParentType

  case class Child2() extends ParentType

  def count(items: List[ParentType]): Either[Error, Int] = {
    items match {
      case e: List[Child1] => Right(items.length)
      case e: List[Child2] =>
        Left("Error no child 2")
      case _ => Left("I only count child 1")
    }
  }

  println(count(List(Child1(),Child1(),Child1())))
  println(count(List(Child2(),Child2(),Child2())))

}

Here I was expecting second print to be Left("Error no child 2") and not Right(3)

Alexander Konovalov
@alexknvl
Apr 11 2017 22:00
Generics are erased at runtime.
Which means that case e: List[Child1] generates the same exact check as case e: List[Child2]
Use (G)ADTs if you need to recover type information
Pattern matching of this form case e: T is evil and should be used as sparingly as possible
Ideally all of your pattern matches should be case Foo(...) =>
Alexander Konovalov
@alexknvl
Apr 11 2017 22:05
I wish types were also erased (e.g. no unrestricted isInstanceOf).
Edmund Noble
@edmundnoble
Apr 11 2017 22:06
Is it time to make @parametric?
Alexander Konovalov
@alexknvl
Apr 11 2017 22:06
parametric-scalac
Parametricity should be the default, not a flag IMO.
@nonparametric please
You want a good default (insert a list of desired properties for CHL-correspondence here) and escape hatches, not the other way around :)
Guillaume Martres
@smarter
Apr 11 2017 22:12
There's been some discussion of parametricity (and a million other things) at https://github.com/lampepfl/dotty/issues/2047#issuecomment-286061895
Alexander Konovalov
@alexknvl
Apr 11 2017 22:21
@smarter I'll read it in a bit, but I can already see coherence being a very nice can of worms because of its interaction with every CHL-unapproved feature :)
Guillaume Martres
@smarter
Apr 11 2017 22:28
please don't punch walls, please discuss things in a positive way instead, we're all trying to make things better
Edmund Noble
@edmundnoble
Apr 11 2017 22:29
Sorry. I'll try to do better.
I'm just frustrated. This is a problem we've spent so much time on and I still have not heard any points against Scato.
Guillaume Martres
@smarter
Apr 11 2017 22:31
maybe because Scato's approach has not been publicized enough?
Edmund Noble
@edmundnoble
Apr 11 2017 22:32
I don't think so. Cats' maintainers who discuss typeclass encodings should all know what it is. But perhaps making a formal proposition is the best way.
To drum up some criticism that is.
Pascal Voitot
@mandubian
Apr 11 2017 22:42
@alexknvl no unrestricted isInstanceOfWDYM by that?
Alexander Konovalov
@alexknvl
Apr 11 2017 23:56
@mandubian You need isInstanceOf for ADT matching