Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Welcome to Scala.next (pre-alpha, git-hash: unknown) (OpenJDK 64-Bit Server VM, Java 1.8.0_131).
- Type in expressions to have them evaluated.
- Type :help for more information.
- scala> class Ref[T] {
- private[this] var value: T = _
- def getValue(): T = value
- def setValue(v: T): Unit = {
- println(s"setValue($v)")
- value = v
- }
- }
- defined class Ref
- scala> def test(ref: Ref[_]) = ref.setValue(ref.getValue())
- def test(ref: Ref[_]): Unit
- scala> val ref1 = new Ref[Int]
- val ref1: Ref[Int] = Ref@3a5c2626
- scala> ref1.setValue(666)
- setValue(666)
- scala> test(ref1)
- setValue(666)
- scala> val refA: Ref[_] = new Ref[String]
- val refA: Ref[_] = Ref@feb98ef
- scala> val refB: Ref[_] = new Ref[Any]
- val refB: Ref[_] = Ref@8cc8cdb
- scala> refA.setValue(refA.getValue())
- setValue(null)
- scala> refA.setValue(refB.getValue())
- -- [E007] Type Mismatch Error: <console>:8:27 ----------------------------------
- 8 |refA.setValue(refB.getValue())
- | ^^^^^^^^^^^^^^^
- | found: refB.T
- | required: refA.T
- |
- scala> val refA2: refA.type = refA
- val refA2: refA = Ref@feb98ef
- scala> refA.setValue(refA2.getValue())
- setValue(null)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement