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

Scala example source code file (depmet_implicit_oopsla_session_simpler.scala)

This example Scala source code file (depmet_implicit_oopsla_session_simpler.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, b, b, bdual, dual, dual, in, int, out, out, session, session, stop, unit

The Scala depmet_implicit_oopsla_session_simpler.scala source code

object Sessions {
  trait Session {
    type Dual <: Session

    def run(dp: Dual): Unit
  }

  sealed case class Stop extends Session {
    type Dual = Stop

    def run(dp: Dual): Unit = {}
  }

  // can't write B <: Session{type Dual = BDual} due to limitations in type inference algorithm
  // (type variables cannot occur on both sides of <:)
  // using B#Dual instead of BDual is too imprecise, since it is disconnected from the actual argument that is passed for B
  // would be nice if we could introduce a universal quantification over BDual that is not part of the 
  // type parameter list
  sealed case class In[A, B <: Session, BDual <: Session](recv: A => B)(implicit dual: B <:< Session{type Dual=BDual}) extends Session {
    type Dual = Out[A, BDual]

    def run(dp: Dual): Unit = recv(dp.data) run dp.cont
  }

  sealed case class Out[A, B <: Session](data: A, cont: B) extends Session {
     type Dual = In[A, cont.Dual, cont.Dual#Dual]

     def run(dp: Dual): Unit = cont run dp.recv(data)
  }

  def addServer =
    In{x: Int => 
    In{y: Int => System.out.println("Thinking")
    Out(x+y,
    Stop())}}

  def addClient =
    Out(3,
    Out(4, { System.out.println("Waiting")
    In{z: Int => System.out.println(z)
    Stop()}}))

  def myRun = addServer run addClient
}

Other Scala examples (source code examples)

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