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

Scala example source code file (typesafecons.scala)

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

cons, cons, doesn't, end, end, first, nothing, pair, pair, second, t, t1, t2, t2

The Scala typesafecons.scala source code

object Pair {
 sealed trait Pair {
   type First
   type Second <: Pair
 }

 class End extends Pair {
   type First = Nothing
   type Second = End

   def ::[T](v : T) : Cons[T, End] = Cons(v, this)
 }

 case object End extends End

 final case class Cons[T1, T2 <: Pair](_1 : T1, _2 : T2) extends Pair {
   type First = T1
   type Second = T2

   def ::[T](v : T) : Cons[T, Cons[T1, T2]] = Cons(v, this)
   def find[T](implicit finder : Cons[T1, T2] => T) = finder(this)
 }

 implicit def findFirst[T1, T2 <: Pair] : Cons[T1, T2] => T1 = (p : Cons[T1, T2]) => p._1
 implicit def findSecond[T, T1, T2 <: Pair](implicit finder : T2 => T) : Cons[T1, T2] => T = (p : Cons[T1, T2]) => finder(p._2)

 val p : Cons[Int, Cons[Boolean, End]] = 10 :: false :: End
// val x : Boolean = p.find[Boolean](findSecond(findFirst))
 val x2 : Boolean = p.find[Boolean]  // Doesn't compile
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala typesafecons.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.