Skip to content

Instantly share code, notes, and snippets.

@leifwickland
Created July 18, 2011 17:43
Show Gist options
  • Save leifwickland/1090136 to your computer and use it in GitHub Desktop.
Save leifwickland/1090136 to your computer and use it in GitHub Desktop.
Recommended way of pimping Scala types (rather than using implicit conversions) according to Jorge's laws in http://dl.dropbox.com/u/1679797/Scalathon/CanBeConfusing.pdf
class ByteArrayWrapper(bytes: Array[Byte]) {
def fromUtf8: String = {
"Converted to String."
}
}
object ByteArrayWrapper {
implicit def makeByteArrayWrapper(bytes: Array[Byte]): ByteArrayWrapper = new ByteArrayWrapper(bytes)
}
object byteImplicit extends App {
import ByteArrayWrapper._
println("start")
val b = Array[Byte](32,30)
println("b = " + b.fromUtf8)
println("done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment