logo       

Re: How do I pattern match the following: msg#00442

lang.scala

Subject: Re: How do I pattern match the following

<whine>
Bummer, dude!

Any chance of enhancing the compiler to implicitly (yikes, there's that word again) mix in a trait for classes with types such that the instance's type is discoverable at runtime:

class Foo[T] {
...
}

val x = new Foo[String]
x.$types$ // List(classOf[String])

That might address the type erasure problem, not break compatibility with Java code and cost a single object reference (4 or 8 bytes) to a List that will likely be shared by a lot of instances.

</whine>

Burak Emir wrote:
Hi David,

all generic types F[T] become simple class types F at runtime, so we can not check if it was F[Dog] or F[Cat]. I posted something about the impact of type erasure a while ago, you can get some details at http://lamp.epfl.ch/~emir/bqbase/2006/10/16/erasure.html

To get rid of the warning, you can write F[_], in your case below, that'd be Seq[_].

You could also write Seq[a], the a most be lower case and the compiler does as if that was the a that will be there at runtime, which is sometimes useful for calling polymorphic methods recursively.

hope this helps,
Burak

On 1/27/07, David Pollak <dpp@xxxxxxxxxx> wrote:
Very cool... next:

pony:~ dpp$ fsc -unchecked Foo.scala
/Users/dpp/Foo.scala:5 warning: non variable type-argument scala.xml.Node in type pattern is unchecked since it is eliminated by erasure
    case s @ Some(n : Seq[Node]) => "Moo"
                      ^
one warning found
pony:~ dpp$ 

Sorry for asking such questions, but I'm trying to clean up some warnings in my code.

On Jan 27, 2007, at 1:05 AM, martin odersky wrote:

David,

Instead of 

              case s : Some[NodeSeq] => s

you can write:

           case s @ Some(x: NodeSeq) => s

That works, because it uses two value patterns: The outer one
one Some, the inner one on NodeSeq.

Cheers

 -- Martin


--
David Pollak







--
Burak Emir
Research Assistant / PhD Candidate
Programming Methods Group
EPFL, 1015 Lausanne, Switzerland
http://lamp.epfl.ch/~emir
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise