-
-
Save galderz/bbd07b608ace868d4794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface JavaAsyncFile extends JavaWriteStream<JavaAsyncFile> { | |
JavaAsyncFile flush(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface JavaWriteStream<T> { | |
T write(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints: