This page is no longer maintained — Please continue to the home page at www.scala-lang.org

No inference to least upper bound in 'or' pattern ?

No replies
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Hello,

Why in that snippet, the inferencer does not pick type B ?

And as it does not seems to work, is there a way to bind a variable to several type (well, to the least upper bound type of several types) ?


=========================
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_02).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait A
defined trait A

scala> trait B extends A
defined trait B

scala> trait X extends B
defined trait X

scala> trait Y extends B
defined trait Y

scala> trait Z extends B
defined trait Z

scala> def foo(x:B) = "foo"
foo: (x: B)B

scala> def bar(plop: A) = plop match { case q@(_:X | _:Y) => foo(q) ; case z:Z => "Z"; case _ => "bar" }
<console>:12: error: type mismatch;
 found   : A
 required: B
       def bar(plop: A) = plop match { case q@(_:X | _:Y) => foo(q) ; case _ => "bar" }
                                                                 ^
======================

-- 
Francois ARMAND
http://fanf42.blogspot.com
http://www.normation.com

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland