Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type extraction in Scala

I am pretty new to Scala and advanced programming languages. I try to solve the following problem.

I have got:

val s: Seq[SomeMutableType[_]]

I assume that all elements in the sequence are of the same type (but do not know which one at this point).

How may I call :

def proc[T](v0: SomeMutableType[T], v1: SomeMutableType[T]) { /* ... */ }

with something like

proc(s(0), s(1))

The compiler complains :

  • type mismatch; found : SomeMutableType[_$351] where type _$351 required: SomeMutableType[Any] Note: _$351 <: Any, but class SomeMutableType is invariant in type T. You may wish to define T as +T instead. (SLS 4.5)

I thought about that covariant thing, but I do not believe it makes sense in my case. I just want the compiler believe me when I say that s(0) and s(1) are of the same type! I usually do this via some casting, but I cannot cast to SomeMutableType[T] here since T is unknown due to erasure. Of course, I cannot change the definition of proc.

like image 492
scand1sk Avatar asked May 09 '11 18:05

scand1sk


People also ask

What is extractor in Scala?

In Scala Extractor is defined as an object which has a method named unapply as one of its part. This method extracts an object and returns back the attributes. This method is also used in Pattern matching and Partial functions.

What is apply and Unapply method?

Whereas the apply method is like a constructor which takes arguments and creates an object, the unapply takes an object and tries to give back the arguments. This is most often used in pattern matching and partial functions. The apply method creates a CustomerID string from a name .

What is pattern matching in Scala?

Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the switch statement in Java and it can likewise be used in place of a series of if/else statements.

What is apply in Scala?

In mathematics and computer science, apply is a function that applies a function to arguments. In Scala, functions, similar to other objects, have a type. We can define a function that takes one argument: // explicitly define type val doubleFunction : Function1[Int,Int] = x => 2 * x.


1 Answers

The problem is that you truly cannot make such a guarantee. For example:

scala> import scala.collection.mutable.Buffer
import scala.collection.mutable.Buffer

scala> val s: Seq[Buffer[_]] = Seq(Buffer(1), Buffer("a"))
s: Seq[scala.collection.mutable.Buffer[_]] = List(ArrayBuffer(1), ArrayBuffer(a))

See? You don't know that s(0) and s(1) are of the same type, because they may not be of the same type.

At this point, you should ask a question about what you want to accomplish, instead of asking how to solve a problem in how you want to accomplish it. They way you took won't work. Step back, think what problem you were trying to solve with this approach, and ask how to solve that problem.

For instance, you say:

I assume that all elements in the sequence are of the same type (but do not know which one at this point).

It may be that what you want to do is parameterize a class or method, and use its type parameter when declaring s. Or, maybe, not have an s at all.

like image 110
Daniel C. Sobral Avatar answered Sep 24 '22 13:09

Daniel C. Sobral