ClassTag in Scala - scala

In Scala, during type erasure, the generic variable is replaced by 'Object' type wherever the generic variable appears in type position.
E.G: val x T; --> is replaced by val x Object;
Due to this, the details of exact type which is passed, will become unavailable during runtime.
To overcome this (to get the exact type during runtime), it is mentioned that ClassTag will help us.
Can you please help me how ClassTag gets the type information during runtime ?
When ClassTag is mentioned , that is also written with a generic type in context bound.
E.G: def method[T:ClassTag] {...}
So I think, the 'T' mentioned here too, will be erased. But ClassTag somehow keeps the type info.
I am bit puzzled how this works.. (similarly TypeTag and WeakTag as well)

This is not correct:
E.G: val x T; --> is replaced by val x Object;
The type of x is not lost; you can look at the value of x at runtime and determine what type it is. Objects retain their runtime type.
Type erasure affects values of a parameterised type Y[T]. The runtime holds a single type for an object so it cannot hold T as well as Y and "erases" T from the type. If I have an instance of Y[T] I can tell at runtime that it is type Y but cannot tell which T it was parameterised with.
Thus I can distinguish List[T] from Vector[T] but cannot distinguish List[T] from List[U]. But an element of that List retains its type and can be matched against T or U. And a member val x: T can be matched directly to determine the type of the object.
A ClassTag is value that represents a type, so if you store the ClassTag of T in your object then you can match that to work out the type of T without having to look at any of the values of type T inside the object. You are explicitly storing the type information for T that was previously erased.
[ Useful discussion in the comments about this really being about classes rather than types ]

Related

How are primitives types objects in Scala?

How are primitive types in Scala objects if we do not use the word "new" to instantiate the instances of those primitives? Programming in Scala by Martin Odersky described the reasoning as some enforcing by a "trick" that makes these value classes to be defined abstract and final, which did not quite make sense to me because how are we able to make an instance of these classes if its abstract? If that same primitive literal is to be stored somewhere let's say into a variable will that make the variable an object?
I assume that you use scala 2.13 with implementation of literal types. For this explanation you can think of type and class as synonyms, but in reality they are different concepts.
To put it all together it worth to treat each primitive type as a set of subtypes each of which representing type of one single literal value.
So literal 1 is a value and type at the same time (instance 1 of type 1), and it is subtype of value class Int.
Let's prove that 1 is subtype of Int by using 'implicitly':
implicitly[1 <:< Int] // compiles
The same but using val:
val one:1 = 1
implicitly[one.type <:< Int] // compiles
So one is kind of an instance (object) of type 1 (and instance of type Int at the same time because because Int is supertype of 1). You can use this value the same way as any other objects (pass it to function or assign to other vals etc).
val one:1 = 1
val oneMore: 1 = one
val oneMoreGeneric: Int = one
val oneNew:1 = 1
We can assume that all these vals contain the same instance of one single object because from practical perspective it doesn't actually matter if this is the same object or not.
Technically it's not an object at all, because primitives came form java (JVM) world where primitives are not objects. They are different kind of entities.
Scala language is trying to unify these two concepts into one (everything is classes), so developers don't have to think too much about differences.
But here are still some differences in a backstage. Each value class is a subtype of AnyVal, but the rest of the classes are subtype of AnyRef (regular class).
implicitly[1 <:< AnyVal] //compiles
implicitly[Int <:< AnyVal] // compiles
trait AnyTraint
implicitly[AnyTraint <:< AnyVal] // fails to compail
implicitly[AnyTraint <:< AnyRef] // compiles
And in addition, because of its non-class nature in the JVM, you can't extend value classes as regular class or use new to create an instance (because scala compiler emulates new by itself). That's why from perspective of extending value classes you should think about them as final and from perspective of creating instances manually you should think of them as abstract. But form most of the other perspectives it's like any other regular class.
So scala compiler can kind of extend Int by 1,2,3 .. types and create instances of them for vals, but developers can't do it manually.

why redefine type variable in Predef.scala?

Why are these type aliases and vals introduced in Predef?
Things that are in Predef are automatically imported. In Scala, you can write
val mySet : Set[String] = Set( "cat", "dog", "poop" )
without having to first write
import scala.collection.immutable.Set
Note that the declaration I wrote above might have been written equivalently as
val mySet : Set[String] = Set.apply( "cat", "dog, "poop" )
On the right-hand side of the equals sign, the word Set refers to a singleton object. We can only call methods on objects (whether singletons or instances of classes). We can't call methods on types. Somehow, we must have autoimported the name of an object Set. This is what the Predef declaration
val Set = immutable.Set
does.
If you are a Java programmer, you can think of a val declaration that just points to an object that would otherwise require the use of an import or a much longer name as being the equivalent of import static.
The Set[String] after the colon and before the equals sign is a type annotation. In this context Set[String] is a type. An object is not a type. If all we had declared in Predef were the val, our declaration could not compile. We would have been saying that mySet's type is a some particular object, but a type is very different from an object, a type is a description of a category to which an object may belong.
To let Set also serve as a type, we need the type alias
type Set[A] = immutable.Set[A]
If you are a java programmer, this functions similarly to a nonstatic import of a type name. (Scala has imports as well, but they must be scoped to a specific block or file. type aliases in Predef are available to all Scala files, so it does much more than an import of the type would do.)
The package scala.collection.immutable contains both an object, declared as
object Set{ ... }
and a type, declared as
trait Set[A]{ ... }
If we want to be able to use both the object (with its useful factory methods) and the type (the compile-time description of the objects our factory creates), we need to make both of them, object and type, available to our code. The two lines (for Set) that you quote above do precisely that. (And the other two lines do precisely the same for Map.)
These types and vals just make importing unnecessary, I believe.
The vals are assigning the companion objects into the vals - again, getting rid of an import.
I suspect they are done in this odd way to avoid a circular compile-time dependency between Predef.scala and Map/Set.scala, but I'm only guessing.
The whole point of Predef is to avoid explicit qualifications and import statements. From the docs:
The Predef object provides definitions that are accessible in all
Scala compilation units without explicit qualification.
Predef provides type aliases for types which are commonly used, such as the immutable collection types scala.collection.immutable.Map,
scala.collection.immutable.Set, and the
scala.collection.immutable.List constructors
(scala.collection.immutable.:: and scala.collection.immutable.Nil).
This way, you can refer to Map without having to explicitly qualify or import scala.collection.immutable.Map.

Scala reflection: TypeTag from context bound doesn't match type parameter

Let's start with an example code:
import scala.reflect.runtime.universe._
class A[T] {
}
def foo[T: TypeTag](a: A[T]) {
println(typeTag[T])
}
val a = new A[Int]
val b: A[_] = a
foo(a)
foo(b)
The output is:
TypeTag[Int]
TypeTag[_$1]
Well, I have no clue whatsoever what is TypeTag[_$1], but it sure looks fancy. :) Still, I would think foo's expectations of the world are being violated here, as in if foo gets called with an instance of A[Int] then it is guaranteed to have the typetag for Int, and not some other obscure typetag. Also, shouldn't typetags be "concrete" in contrast to weektypetags?
Now of course I see that the compiler cannot tell in the second foo call the type parameter of A. So my expectation wasn't that I magically get typeTag[Int] as the output of the second call, but rather I was hoping for a compile time error.
Where is my missunderstanding?
Some futile speculation
One could argue that what's going on is a call to foo with argument type A[_], or, to make it more explicit, the existential type A[S] forSome { type S }. An then the typeTag captures the "_" somehow. But this doesn't really make sense, as foo expects A[T] for some concrete T, and the above existential type is not A[T] for any concrete T.
Or maybe a call with an argument A[Any]? But then why not TypeTag[Any], and even more severely, A is not covariant, so this would also be plain wrong.
Btw, I'm using scala 2.10.
Type tags are created at compile time, and values are not available at compile time. Once you cast an A[Int] to the existential type A[_], all information about the type parameter has been lost.
If they were created at run-time, based on values, type erasure would make it impossible to know A's parameter. Even something that is known to be an A[Int] at compile-time is at best an A[Object] at run-time (unless A is Array, but let's not go there).
So your speculation is correct, for foo(b), the type parameter is indeed the _ from the existential A[_], and that anonymous type variable prints as _$1.

what does A#B mean in scala

I am trying to understand the following piece of code. but I don't know what the R#X mean. could someone help me?
// define the abstract types and bounds
trait Recurse {
type Next <: Recurse
// this is the recursive function definition
type X[R <: Recurse] <: Int
}
// implementation
trait RecurseA extends Recurse {
type Next = RecurseA
// this is the implementation
type X[R <: Recurse] = R#X[R#Next]
}
object Recurse {
// infinite loop
type C = RecurseA#X[RecurseA]
}
You may gain type from existing instance of a class:
class C {
type someType = Int
}
val c = new C
type t = c.someType
Or may address to the type directly without instantiating an object: C#someType This form is very usefull for type expressions where you have no space to create intermediate variables.
Adding some clarifications as it was suggested in comments.
Disclaimer: I have only partial understanding of how Scala's type system works. I'd tried to read documentation several times, but was able to extract only patchy knowledges from it. But I have rich experience in scala and may predict compilers behavior on individual cases well.
# called type projection and type projection compliments normal hierarchical type access via . In every type expression scala implicitly uses both.
scala reference gives examples of such invisible conversions:
t ə.type#t
Int scala.type#Int
scala.Int scala.type#Int
data.maintable.Node data.maintable.type#Node
As use see, every trivial usage of type projection actually works on type (that is return with .type) not on an object. The main practical difference (I'm bad with definitions) is that object type is something ephemeral as object itself is. Its type may be changed in appropriate circumstances such as inheritance of an abstract class type. In contrast type's type (the definition of the type projection) is as stable as sun. Types (don't mix them with classes) in scala are not first-class citizens and can not be overridden further.
There are different places suitable for putting type expression into. There are also some places where only stable types are allowed. So basically type projection is more constant for terms of type.

"Parameter type in structural refinement may not refer to an abstract type defined outside that refinement"

When I compile:
object Test extends App {
implicit def pimp[V](xs: Seq[V]) = new {
def dummy(x: V) = x
}
}
I get:
$ fsc -d aoeu go.scala
go.scala:3: error: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement
def dummy(x: V) = x
^
one error found
Why?
(Scala: "Parameter type in structural refinement may not refer to an abstract type defined outside that refinement" doesn't really answer this.)
It's disallowed by the spec. See 3.2.7 Compound Types.
Within a method declaration in a structural refinement, the type of any value parameter may only refer to type parameters or abstract types that are contained inside the refinement. That is, it must refer either to a type parameter of the method
itself, or to a type definition within the refinement. This restriction does not apply
to the function’s result type.
Before Bug 1906 was fixed, the compiler would have compiled this and you'd have gotten a method not found at runtime. This was fixed in revision 19442 and this is why you get this wonderful message.
The question is then, why is this not allowed?
Here is very detailed explanation from Gilles Dubochet from the scala mailing list back in 2007. It roughly boils down to the fact that structural types use reflection and the compiler does not know how to look up the method to call if it uses a type defined outside the refinement (the compiler does not know ahead of time how to fill the second parameter of getMethod in p.getClass.getMethod("pimp", Array(?))
But go look at the post, it will answer your question and some more.
Edit:
Hello list.
I try to define structural types with abstract datatype in function
parameter. ... Any reason?
I have heard about two questions concerning the structural typing
extension of Scala 2.6 lately, and I would like to answer them here.
Why did we change Scala's native values (“int”, etc.) boxing scheme
to Java's (“java.lang.Integer”).
Why is the restriction on parameters for structurally defined
methods (“Parameter type in structural refinement may not refer
to abstract type defined outside that same refinement”) required.
Before I can answer these two questions, I need to speak about the
implementation of structural types.
The JVM's type system is very basic (and corresponds to Java 1.4). That
means that many types that can be represented in Scala cannot be
represented in the VM. Path dependant types (“x.y.A”), singleton types
(“a.type”), compound types (“A with B”) or abstract types are all types
that cannot be represented in the JVM's type system.
To be able to compile to JVM bytecode, the Scala compilers changes the
Scala types of the program to their “erasure” (see section 3.6 of the
reference). Erased types can be represented in the VM's type system and
define a type discipline on the program that is equivalent to that of
the program typed with Scala types (saving some casts), although less
precise. As a side note, the fact that types are erased in the VM
explains why operations on the dynamic representation of types (pattern
matching on types) are very restricted with respect to Scala's type
system.
Until now all type constructs in Scala could be erased in some way.
This isn't true for structural types. The simple structural type “{ def
x: Int }” can't be erased to “Object” as the VM would not allow
accessing the “x” field. Using an interface “interface X { int x{}; }”
as the erased type won't work either because any instance bound by a
value of this type would have to implement that interface which cannot
be done in presence of separate compilation. Indeed (bear with me) any
class that contains a member of the same name than a member defined in
a structural type anywhere would have to implement the corresponding
interface. Unfortunately this class may be defined even before the
structural type is known to exist.
Instead, any reference to a structurally defined member is implemented
as a reflective call, completely bypassing the VM's type system. For
example def f(p: { def x(q: Int): Int }) = p.x(4) will be rewritten
to something like:
def f(p: Object) = p.getClass.getMethod("x", Array(Int)).invoke(p, Array(4))
And now the answers.
“invoke” will use boxed (“java.lang.Integer”) values whenever the
invoked method uses native values (“int”). That means that the above
call must really look like “...invoke(p, Array(new
java.lang.Integer(4))).intValue”.
Integer values in a Scala program are already often boxed (to allow the
“Any” type) and it would be wasteful to unbox them from Scala's own
boxing scheme to rebox them immediately as java.lang.Integer.
Worst still, when a reflective call has the “Any” return type,
what should be done when a java.lang.Integer is returned? The called
method may either be returning an “int” (in which case it should be
unboxed and reboxed as a Scala box) or it may be returning a
java.lang.Integer that should be left untouched.
Instead we decided to change Scala's own boxing scheme to Java's. The
two previous problems then simply disappear. Some performance-related
optimisations we had with Scala's boxing scheme (pre-calculate the
boxed form of the most common numbers) were easy to use with Java
boxing too. In the end, using Java boxing was even a bit faster than
our own scheme.
“getMethod”'s second parameter is an array with the types of the
parameters of the (structurally defined) method to lookup — for
selecting which method to get when the name is overloaded. This is the
one place where exact, static types are needed in the process of
translating a structural member call. Usually, exploitable static types
for a method's parameter are provided with the structural type
definition. In the example above, the parameter type of “x” is known to
be “Int”, which allows looking it up.
Parameter types defined as abstract types where the abstract type is
defined inside the scope of the structural refinement are no problem
either:
def f(p: { def x[T](t: T): Int }) = p.xInt
In this example we know that any instance passed to “f” as “p” will
define “x[T](t: T)” which is necessarily erased to “x(t: Object)”. The
lookup is then correctly done on the erased type:
def f(p: Object) = p.getClass.getMethod("x", Array(Object)).invoke(p,
Array(new java.lang.Integer(4)))
But if an abstract type from outside the structural refinement's scope
is used to define a parameter of a structural method, everything breaks:
def f[T](p: { def x(t: T): Int }, t: T) = p.x(t)
When “f” is called, “T” can be instantiated to any type, for example:
f[Int]({ def x(t: Int) = t }, 4)
f[Any]({ def x(t: Any) = 5 }, 4)
The lookup for the first case would have to be “getMethod("x",
Array(int))” and for the second “getMethod("x", Array(Object))”, and
there is no way to know which one to generate in the body of
“f”: “p.x(t)”.
To allow defining a unique “getMethod” call inside “f”'s body for
any instantiation of “T” would require any object passed to “f” as the
“p” parameter to have the type of “t” erased to “Any”. This would be a
transformation where the type of a class' members depend on how
instances of this class are used in the program. And this is something
we definitely don't want to do (and can't be done with separate
compilation).
Alternatively, if Scala supported run-time types one could use them to
solve this problem. Maybe one day ...
But for now, using abstract types for structural method's parameter
types is simply forbidden.
Sincerely,
Gilles.
Discovered the problem shortly after posting this: I have to define a named class instead of using an anonymous class. (Still would love to hear a better explanation of the reasoning though.)
object Test extends App {
case class G[V](xs: Seq[V]) {
def dummy(x: V) = x
}
implicit def pimp[V](xs: Seq[V]) = G(xs)
}
works.