|
Re: Type inferencing pattern matching anonymous functions: msg#00468lang.scala
Replying to my own question, just to make sure the solution is archived. On 1/26/07, Rafael de F. Ferreira <rafael@xxxxxxxxxxxxxxxxxx> wrote: I hit a problem when dealing with {case P => R ...} expressions andI do :-) Inspired by Martin's message to the list explaining the foldLeft syntax, I changed the method declaration to "explicitly" curry the second parameter. This way the compiler is able to infer the correct type of the anonymous function being passed. It ended up like this: def replace[T](l:List[T])(f:PartialFunction[T,T]) : List[T] = l match { case Nil => Nil case x::xs => (if (f.isDefinedAt(x)) f(x) else x) :: replace(xs)(f); } case class Something(i:int) val original = List(Something(1), Something(2), Something(3)) val double1 = replace(original){case Something(2) => Something(4)}
-- Rafael de F. Ferreira. http://www.rafaelferreira.net/ |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: junit and eclipse plugin: 00468, David Pollak |
|---|---|
| Next by Date: | Prioritizing message handling in actors: 00468, David Pollak |
| Previous by Thread: | Type inferencing pattern matching anonymous functionsi: 00468, Rafael de F. Ferreira |
| Next by Thread: | more fun with packing: 00468, Judson, Ross |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |