Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surprising equivalences and non-equivalences regarding this.type

It appears to make a difference whether you refer to this.type from inside a Trait or from the scope where the object is created, with surprising results.

import scala.reflect.runtime.universe._

trait Trait {
  val ttag = typeOf[this.type]
  println(s"Trait constructor: $this")
}

object Instance1 extends Trait

object Instance2 extends Trait

println(typeOf[Instance1.type] =:= typeOf[Instance2.type])  // Should be false
println(Instance1.ttag =:= Instance2.ttag)                  // Should be false
println(Instance1.ttag =:= typeOf[Instance1.type])          // Should be true

Here's the output:

false    // As expected: the singleton types of two objects are different.
Trait constructor: $line9.$read$$iw$$iw$$iw$$iw$Instance1$@58c46295
Trait constructor: $line10.$read$$iw$$iw$$iw$$iw$Instance2$@452451ba
true     // But the this.type tags are equivalent
false    // and the this.type tag is not equivalent to the singleton type.

So, there are two distinct objects, but apparently each is getting an equivalent type tag for its this.type, which is not equivalent to the .type of the object as seen from an enclosing scope.

Is this a compiler bug, or, if not, could you explain why this behavior makes sense?

(I'm running Scala 2.11.2. I've tried it with a self alias for this, with the same result.)

like image 857
Ben Kovitz Avatar asked Aug 18 '15 22:08

Ben Kovitz


People also ask

What is equivalence and non-equivalence in translation?

"Non-equivalence at word level means that the target language has no direct equivalent for a word which occurs in the source text ". Further, non-equivalence at word level is not much determined by a one to one relationship between word and meaning.

How many types of equivalences are there?

There are two main types of equivalence; qualitative and quantitative. In qualitative there are five types of equivalence; Referential or Denotative, Connotative, Text-Normative, Pragmatic or Dynamic and Textual Equivalence.…

What is equivalence and example?

Equivalence relations are often used to group together objects that are similar, or “equiv- alent”, in some sense. 2 Examples. Example: The relation “is equal to”, denoted “=”, is an equivalence relation on the set of real numbers since for any x, y, z ∈ R: 1. (Reflexivity) x = x, 2.

What are the two types of untranslatability?

Nevertheless, it must be mentioned that there are two types of untranslatability: linguistic and cultural. As for the former one, it occurs when two languages don't share common linguistic expression so that the same meaning is retained.


Video Answer


1 Answers

The following program prints false and then true. To my mind, there should be no material difference between these two cases (though that's really more of an opinion; I can't really say if there's a reason or not):

import scala.reflect.runtime.universe._

object Test2 extends App {
  def foo(): Type = {
    object a
    typeOf[a.type]
  }

  println(foo() =:= foo()) // false

  trait Trait {
    val ttag = typeOf[this.type]
  }

  object Instance1 extends Trait

  object Instance2 extends Trait

  println(Instance1.ttag =:= Instance2.ttag) // true
}
like image 124
Owen Avatar answered Nov 15 '22 06:11

Owen