Serializable Java Lambdas

Recently I was presented with the following error when serializing a lambda with Kryo:

Java
 




x


 
1
com.esotericsoftware.kryo.KryoException: 
2
  java.lang.IllegalArgumentException: 
3
    Unable to serialize Java Lambda expression, unless explicitly declared e.g., 
4
    Runnable r = (Runnable & Serializable) () -> System.out.println("Hello world!");


If you do not recognize the (Runnable & Serializable) syntax, don’t worry, it is merely stating that the lambda must implement two types. This is called Type Intersection. Personally, I have never needed to use this myself, so have never really thought about it. Serializable is a bit of a unique interface in this regards, as there is nothing you actually need to implement.