Scala Programming Language
  1. Scala Programming Language
  2. SI-6517

Use TypeTags when pattern-matching otherwise erased types

    Details

      Description

      When the scrutinee of a match has a parametric type, and appropriate TypeTags are in scope, it should be possible to formulate a match like the following:

      def f[T: TypeTag](x: T) = x match {
        case xs: List[String] => ...
        case xs: List[Int] => ...
      }
      

      It would be nice if that would also work with type parameters used in non-top-level positions:

      def f[T: TypeTag](xs: List[T]) = xs match {
        case (s: String) :: tail =>
      

      In principle the compiler should have all necessary knowledge available, but I cannot judge how difficult it would be to make this work.

      Talking with Adriaan did not result in immediate dismissal, and he asked me to reference SI-5143 as related.

        Activity

        Hide
        Paul Phillips added a comment -

        See also SI-329, which I closed as a duplicate of this, if only to be reminded about checking refinement types at runtime too.

        Show
        Paul Phillips added a comment - See also SI-329 , which I closed as a duplicate of this, if only to be reminded about checking refinement types at runtime too.
        Hide
        Paul Phillips added a comment -

        By the way, I can't tell what you want from the second example. That doesn't need to know anything about T.

        Show
        Paul Phillips added a comment - By the way, I can't tell what you want from the second example. That doesn't need to know anything about T.
        Hide
        Roland Kuhn added a comment -

        You’re right, I missed the mark slightly: make that

        def f[T: TypeTag](xs: List[T]) = xs match {
          case (s: Set[String]) :: tail => ...
          case xs: List[Map[Int, Int]]  => ...
        }
        
        Show
        Roland Kuhn added a comment - You’re right, I missed the mark slightly: make that def f[T: TypeTag](xs: List[T]) = xs match { case (s: Set[String]) :: tail => ... case xs: List[Map[Int, Int]] => ... }
        Hide
        Adriaan Moors added a comment -

        Unassigning and rescheduling to M6 as previous deadline was missed.

        Show
        Adriaan Moors added a comment - Unassigning and rescheduling to M6 as previous deadline was missed.

          People

          • Assignee:
            Unassigned
            Reporter:
            Roland Kuhn
          • Votes:
            2 Vote for this issue
            Watchers:
            11 Start watching this issue

            Dates

            • Created:
              Updated:

              Development