scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Generics and cases #2182

Closed scabug closed 8 years ago

scabug commented 15 years ago

The following code is problematic:

abstract sealed class FinalResult[X]
case class Error(msg:String) extends FinalResult[Nothing]
case class Success[U](value:U) extends FinalResult[U]

class A {
    protected sealed abstract class BaseResult[X]    
    protected case class Result[X](result:X) extends BaseResult[X]
    protected case class NoResult(msg:String) extends BaseResult[Nothing]

    protected abstract class B[U] {              
        protected def get:BaseResult[U]
        def apply:FinalResult[U] = {
            get match {
              case r:NoResult => Error(r.msg)
              case r:Result[U] => Success(r.result)
            }
        }
    }
}

It compiles, but with a warning about type erasure at line

case r:Result[U] => Success(r.result)

However, there doesn't seem to be any way to get rid properly of that warning except by writting

case r:Result[_] => Success(r.result.asInstanceOf[U])

which is long to write and involves an unrequired cast even though we are ensured that r.result cannot be anything but type U.

scabug commented 15 years ago

Imported From: https://issues.scala-lang.org/browse/SI-2182?orig=1 Reporter: yves See #884

scabug commented 15 years ago

@paulp said: This is basically a duplicate of #884 - I've added a pointer from there to here for completeness.

scabug commented 14 years ago

@paulp said: Interestingly this was not really a duplicate of #884. Instead it was something which worked all along. Your real issue was a lack of variance in FinalResult and BaseResult. Yes, I'm aware I closed this 15 months ago and am talking to myself.

scabug commented 8 years ago

Naftoli Gugenheim (naftoligug) said (edited by @paulp on Aug 28, 2016 5:18:01 PM UTC):

I'm confused -- is this really a duplicate of SI-884?

"this was not really a duplicate of SI-884"

If not, what should its status be?

"Not a Bug"

scabug commented 8 years ago

Naftoli Gugenheim (naftoligug) said: @extempore thanks for responding. Next time could you add a comment instead of editing mine? :D I've hit this issue myself. Would you perhaps be so kind as to expand the fix? "Your real issue was a lack of variance in FinalResult and BaseResult" -- do you mean if they were covariant this would work? What does variance have to do with the issue, I don't see any subtype or supertype of X anywhere.

scabug commented 8 years ago

Naftoli Gugenheim (naftoligug) said: trying to change resolution

scabug commented 8 years ago

Naftoli Gugenheim (naftoligug) said: I changed the resolution, I hope that's okay

scabug commented 8 years ago

@paulp said: If it's not obvious, I didn't intend to edit yours. I've only had 8-10 years to master JIRA's intuitive interface, I can't imagine how I could still be making that mistake. Oh wait, yes I can.

scabug commented 8 years ago

@paulp said (edited on Aug 28, 2016 7:08:19 PM UTC): FinalResult[X] is invariant in X. Error extends FinalResult[Nothing]. That means you can't return Error for FinalResult[U] unless U=Nothing.

scabug commented 8 years ago

Naftoli Gugenheim (naftoligug) said (edited on Aug 28, 2016 10:56:26 PM UTC): @Paul Phillips it was obvious, don't worry. :)

(speaking of JIRA UX, I'm trying to figure out the right way to "mention" someone...)

As for the explanation, I see what you mean now. I was too focused on the next line, case r:Result[U] => Success(r.result).

Indeed, this does compile:

case class C[A](a: A, modify: A => A)
def test[A](c: C[A]): C[A] = c match { case C(x, f) => C(f(x), f) }