- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
No inference to least upper bound in 'or' pattern ?
Tue, 2012-02-14, 00:44
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" }
^
======================
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