- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Types of Values Bound by Typed Pattern Matches?
Mon, 2009-03-23, 18:48
Hi,
I just encountered an unexpected overload ambiguity in amethod call from
within a match. The
The code in question is this:
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
def
render(expr: FOL_Expression): Renderer =
{
expr match {
case formula: FOL_Formula => render(formula)
case term: FOL_Term => render(term)
case sym: FOL_Symbol => render(sym) // <-- Yields error shown below
// case sym: FOL_Symbol => render(sym.asInstanceOf[FOL_Symbol]) // Compiles
case _ => render(expr.toString)
}
}
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
Error:Error:line (42)error: ambiguous reference to overloaded definition,
both method render in class Renderer of type (adt.this.FOL_Symbol)io.this.Renderer
and method render in class Renderer of type (adt.this.FOL_Expression)io.this.Renderer
match argument types (adt.this.FOL_Expression with adt.this.FOL_Symbol{})
and expected result type io.this.Renderer
case sym: FOL_Symbol => render(sym)
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
It appears that the type of a value bound by a type pattern in a match
is the type of the value being matched (here, FOL_Expression) "with" the
type in the type pattern (here, FOL_Symbol).
Is that correct?
Randall Schulz
On Mon, Mar 23, 2009 at 1:48 PM, Randall R Schulz <rschulz@sonic.net> wrote: