Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.30 KB | None | 0 0
  1. Welcome to Scala.next (pre-alpha, git-hash: unknown)  (OpenJDK 64-Bit Server VM, Java 1.8.0_131).
  2. Type in expressions to have them evaluated.
  3. Type :help for more information.
  4. scala> class Ref[T] {
  5.            private[this] var value: T = _
  6.            
  7.            def getValue(): T = value
  8.          
  9.            def setValue(v: T): Unit = {
  10.                         println(s"setValue($v)")
  11.                         value = v
  12.                 }
  13.          }
  14. defined class Ref
  15. scala> def test(ref: Ref[_]) = ref.setValue(ref.getValue())
  16. def test(ref: Ref[_]): Unit
  17. scala> val ref1 = new Ref[Int]
  18. val ref1: Ref[Int] = Ref@3a5c2626
  19. scala> ref1.setValue(666)
  20. setValue(666)
  21. scala> test(ref1)
  22. setValue(666)
  23. scala> val refA: Ref[_] = new Ref[String]
  24. val refA: Ref[_] = Ref@feb98ef
  25. scala> val refB: Ref[_] = new Ref[Any]
  26. val refB: Ref[_] = Ref@8cc8cdb
  27. scala> refA.setValue(refA.getValue())
  28. setValue(null)
  29. scala> refA.setValue(refB.getValue())
  30. -- [E007] Type Mismatch Error: <console>:8:27 ----------------------------------
  31. 8 |refA.setValue(refB.getValue())
  32.   |              ^^^^^^^^^^^^^^^
  33.   |              found:    refB.T
  34.   |              required: refA.T
  35.   |              
  36. scala> val refA2: refA.type = refA
  37. val refA2: refA = Ref@feb98ef
  38. scala> refA.setValue(refA2.getValue())
  39. setValue(null)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement