Skip to content

Instantly share code, notes, and snippets.

@galderz
Created December 19, 2013 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galderz/bbd07b608ace868d4794 to your computer and use it in GitHub Desktop.
Save galderz/bbd07b608ace868d4794 to your computer and use it in GitHub Desktop.
object FluentInheritanceImplicits2 extends App {
trait Wrapper[In] {
type Out
def wrap(i:In): Out
}
// implicit def wrap[A](i:A)(implicit wrap: Wrapper[A]): wrap.Out = wrap wrap i
@inline implicit def asWriteStream(a: AsyncFile) = new WriteStream(a.asJava)
implicit val asWriteStreamWrapper = new Wrapper[JavaWriteStream[_]] {
type Out = WriteStream[JavaWriteStream[_]]
def wrap(i: JavaWriteStream[_]): WriteStream[JavaWriteStream[_]] =
new WriteStream[JavaWriteStream[_]](i)
}
implicit val asAsyncFileWrapper = new Wrapper[JavaAsyncFile] {
type Out = AsyncFile
def wrap(i: JavaAsyncFile): AsyncFile = new AsyncFile(i)
}
class AsyncFile(val asJava: JavaAsyncFile) extends AnyVal {
def flush(): AsyncFile = {
asJava.flush()
this
}
}
class WriteStream[T <: JavaWriteStream[_]](val asJava: T) extends AnyVal {
def write()(implicit wrapper: Wrapper[T]): wrapper.Out = {
asJava.write()
wrapper.wrap(asJava)
}
}
private val file = new AsyncFile(new JavaAsyncFile {
def write(): JavaAsyncFile = {
println("I'm java write file")
this
}
def flush(): JavaAsyncFile = {
println("I'm java flush file")
this
}
})
println("Flush, then write:")
file.flush().write()
println("------------------")
println("Write, then flush:")
file.write().flush()
}
public interface JavaAsyncFile extends JavaWriteStream<JavaAsyncFile> {
JavaAsyncFile flush();
}
public interface JavaWriteStream<T> {
T write();
}
@galderz
Copy link
Author

galderz commented Dec 19, 2013

Prints:

Flush, then write:
I'm java flush file
I'm java write file
------------------
Write, then flush:
I'm java write file
I'm java flush file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment