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

Scala example source code file (gadts2.scala)

This example Scala source code file (gadts2.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

array, cell, cell, double, int, mydouble, myint, number, number, numterm, numterm, term, term, test

The Scala gadts2.scala source code

object Test {

  abstract class Number
  case class MyInt(n: Int) extends Number
  case class MyDouble(d: Double) extends Number

  trait Term[+a]
  case class Cell[a](var x: a) extends Term[a]
  final case class NumTerm(val n: Number) extends Term[Number]

  def f[a](t: Term[a], c: Cell[a]) {
    t match {  
      case NumTerm(n) => c.x = MyDouble(1.0) 
    }
  }

  val x: Term[Number] = NumTerm(MyInt(5))

  def main(args: Array[String]) {
    val cell = Cell[Number](MyInt(6))
    Console.println(cell)
    f[Number](new NumTerm(MyInt(5)), cell)
    Console.println(cell)
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.