Skip to content

Instantly share code, notes, and snippets.

@Axure
Last active October 7, 2017 20:21
Show Gist options
  • Save Axure/69776381a8992570c29133fe671e2fca to your computer and use it in GitHub Desktop.
Save Axure/69776381a8992570c29133fe671e2fca to your computer and use it in GitHub Desktop.
Modules in OCaml
trait Lazy[T[_]] {
type Type[X] = T[X]
def mk[A](thunk: () => A): T[A]
}
object LazyThunk extends Lazy[({type Thunk[X] = () => X})#Thunk] {
override def mk[A](thunk: () => A): () => A = thunk
}
trait StreamTrait[Thunk[_]] {
class StreamT[A](val tuple: Lazy[Thunk]#Type[(A, StreamT[A])]) {}
def hd[A](stream: StreamT[A]): A
}
class StreamClass[Thunk[_]](val lazyT: Lazy[Thunk]) extends StreamTrait[Thunk] {
override def hd[A](stream: StreamT[A]): A = ??
}
// now I have to use it as
object App {
def main(args: Array[String]): Unit = {
val S = new StreamClass(LazyThunk)
// S.hd (something), etc.
// Can I make it static, like StreamClass[LazyThunk]? I just want to simulate modules in OCaml.
// Or what is the canonical way of doing this (mimcking typeclass?) in Scala, without using a module like system?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment