Writing With Typeclasses

Details on how output is converted to bytes and how the design can be extended and used

Strings And Characters

Writing Strings and characters require that codec object is passed to the Output object so that means the "normal" typeclass design cannot be used to implicitely write characters and strings to the Output. Because of this write,patch,insert,etc... are overloaded with a typeclass version as well as a version that takes a string.

The result is that writing strings is a simple exercise but writing characters or Traversables of characters is less trivial. The examples below show how to write strings and characters.

    import scalax.io._
    import Resource._

    val out = fromFileString("out")

    // codec can be passed implicitely or explicitly
    out.write("A string")(Codec.UTF8)
    implicit val codec = Codec.UTF8
    out.write("c")

    // out.write('c') will not compile since a converter cannot
    // be resolved by the implicit resolution mechanism because
    // character converters require a codec and only concrete
    // objects are resolved.
    out.write('c')(OutputConverter.charToOutputFunction)
    out.write(Set('a','e','i','o','u'))(OutputConverter.charsToOutputFunction)

    // converters can be passed implicitly
      implicit val traversableCharCoverter = OutputConverter.charsToOutputFunction
      out.write(Set('a','e','i','o','u') )
      out.write('a' to 'z')