Scala default parameters and null

Pattern matching def aMethod(param: String = null) { val paramOrDefault = param match { case null => “asdf” case s => s } } Option (implicitly) def aMethod(param: String = null) { val paramOrDefault = Option(param).getOrElse(“asdf”) } Option (explicitly) def aMethod(param: Option[String] = None) { val paramOrDefault = param getOrElse “asdf” } The last approach … Read more

Passing elements of a List as parameters to a function with variable arguments

Take a look at this example, I will define a function printme that takes vargs of type String def printme(s: String*) = s.foreach(println) scala> printme(List(“a”,”b”,”c”)) <console>:9: error: type mismatch; found : List[String] required: String printme(List(a,b,c)) What you really need to un-pack the list into arguments with the :_* operator scala> val mylist = List(“1″,”2″,”3”) scala> … Read more

The tilde operator in Scala

Firstly, some meta-advice. Any time you’re wondering how the compiler expands some syntactic sugar, infers a type, or applies an implicit conversion, use scala -Xprint:typer -e <expr> to show you what happened. scala -Xprint:typer -e “val a = 2; ~a” … private[this] val a: Int = 2; private <stable> <accessor> def a: Int = $anon.this.a; … Read more

no configuration setting found for key akka

The problem is when using sbt:assembly the default merge strategy excludes all the reference.conf files as per If multiple files share the same relative path (e.g. a resource named application.conf in multiple dependency JARs), the default strategy is to verify that all candidates have the same contents and error out otherwise. The solution is to … Read more