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

Types of Values Bound by Typed Pattern Matches?

1 reply
Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.

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

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Types of Values Bound by Typed Pattern Matches?
It makes sense -- "with" means that it has to conform to both types, which must be true at this point in you code. Should the compiler "forget" that it must conform to the input type?But if the "with" order was reversed, would there still be an ambiguity?

On Mon, Mar 23, 2009 at 1:48 PM, Randall R Schulz <rschulz@sonic.net> wrote:
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

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