alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (variances.scala)

This example Scala source code file (variances.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

a, a, b, b, c, convert, int, st, string, t, t, test, vector, vector

The Scala variances.scala source code

package test

trait Vector[+A] {
  def append(x: Vector[A]): Vector[A] 
  private[this] def append3(x: Vector[A]): Vector[A] = append(x)
}

class C[T]

object Covariant {
  class Foo[+A] {
    private[this] var a : A = _
    def getA : A = a
    private[this] def setA(a : A) = this.a = a 
    
    object Baz extends C[A]
    trait Convert[B] {
      def b2a(b : B) : A
      def doit(b : B) = setA(b2a(b))
    }
  }
  class Foo2[+A] {
    private[this] var a : A = _
    def getA : A = a
    private[this] def setA(a : A) = this.a = a 
    
    {
      trait Convert[B] {
        def b2a(b : B) : A
        def doit(b : B) = setA(b2a(b))
      }
      ()
    }
  }
  class Foo3[+A] {
    private[this] var a : A = _
    def getA : A = a
    private[this] def setA(a : A) = this.a = a 
    
    private[this] trait Convert[B] {
      def b2a(b : B) : A
      def doit(b : B) = setA(b2a(b))
    }
  }
  abstract class AbstractTest {
    val a : Foo[AnyRef]
    val c = new a.Convert[Int] {
      def b2a(b : Int) : AnyRef = "hello"
    }
    val b : Int = 42
  }
  class Test extends AbstractTest {
    val a : Foo[java.lang.Character] = new Foo[java.lang.Character]
  }
  def main(args : Array[String]) {
    val test = new Test
    test.c.doit(test.b)
    val x : java.lang.Character = test.a.getA
    Console.println("XXX " + x)
  }

  abstract class T[+A] {
    val x: T[A] {
      val m: A => A
    }
  }
  object ST extends T[String] {
    val x: T[String] { val m: String => String } = ST
    val m: String => String = (_.substring(1))
  }
  val t: T[Any] = ST
  t.x.m(new Object)
}

object TestAlias {
  class B[-T]
  trait C[+T] {
    type A = T
    def foo: B[A]
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala variances.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.