lunaryorn

scala

#scala

How not to serialize binary data:

import io.circe._
import com.google.protobuf.ByteString

final case class Message(body: ByteString)

object Message {
  implicit val encodeMessage: Encoder[Message] =
    Encoder.instance { message =>
      val array = message.body.toByteArray
        .map(_.toInt)
        .map(Json.fromInt)
      Json.fromValues(array)
    }
}

Sadly, I actually saw this today… I didn’t do a git blame; I guess I’d never be able to look at that person again 😢

#scala

TIL that you can declare a (case) class in a method body. In other words the following is valid Scala code:

def foo(s: String): Unit = {
  final case class Foo(i: Int)
}

Even after six years Scala continues to surprise.