February, 2015

Tiny types scala edition

Previously I wrote about generating value type wrappers on top of C# primitives for better handling of domain level knowledge. This time I decided to try it out in scala as I’m jumping into the JVM world.

With scala we don’t have the value type capability that c# has, but we can sort of get there with implicits and case classes.

The simple gist is to generate stuff like

package com.devshorts.data

case class foo(data : String)
case class bar(data : String)
case class bizBaz(data : Int)
case class Data(data : java.util.UUID)

And the implicit conversions

package com.devshorts.data

object Conversions{
    implicit def convertfoo(i : foo) : String = i.data
    implicit def convertbar(i : bar) : String = i.data
    implicit def convertbizBaz(i : bizBaz) : Int = i.data
    implicit def convertData(i : Data) : java.util.UUID = i.data

Now you get a similar feel of primitive wrapping with function level unboxing and … Read more

Simple log context wrapper

I’m still toying around with the scala play! framework and I wanted to check out how I can make logging contextual information easy. In the past with .NET I’ve used and written libraries that wrap the current log provider and give you extra niceties with logging. One of my favorites was being able to do stuff like

var foo = "1";
var bar = "2";
logger.With(new { foo, bar }).Info("data")

Which would output a log line like

data, foo=1; bar=2

The logger’s “with” was even chainable so you could capture a previously built “with” context and re-use it. It was really nice when you want to create a baseline logging context for complex functions.

Java doesn’t have the concept of anonymous classes and I’m not sure you can do optimizations with reflection like you can with .NET (creating reflective based property invokers into delegates).

Either way, this makes for a … Read more