How to substitute an empty string (or null) for a concise default string in Scala

advertisements

I have a method that returns a String. I want to substitute it with a default value such as "<empty>" if it returns an empty string or null. Let's assume its name is getSomeString, it is an expensive operation so I can call it only once, and I can't change its return type to Option[String]. For now, I'm doing the following:

val myStr = {
  val s = getSomeString
  if (s == null || s.isEmpty) "<empty>" else s
}

Is there a simpler way to achieve the same thing?


Given an expensive function:

scala> def s(i: Int): String = i match { case 0=>null case 1=>"" case 2=>"hi" }
s: (i: Int)String

I think this is easy to read and free of overhead, cf this in the wild:

scala> def q(i: Int) = s(i) match { case ""|null => "<empty>" case x => x }
q: (i: Int)String

scala> q(0)
res3: String = <empty>

scala> q(1)
res4: String = <empty>

scala> q(2)
res5: String = hi

To my eyes, this is not as expressive, even with minimalist punctuation:

scala> Option(s(0)) filterNot (_.isEmpty) getOrElse "<empty>"
res6: String = <empty>

Moreover, contrast the cost in anonfun classes for the closures and additional method invocations:

scala> :javap -
  Size 1161 bytes
  MD5 checksum 765f5f67b0c574252b059c8adfab1cf0
  Compiled from "<console>"
[...]
         9: getstatic     #26                 // Field scala/Option$.MODULE$:Lscala/Option$;
        12: getstatic     #31                 // Field .MODULE$:L;
        15: iconst_0
        16: invokevirtual #35                 // Method .s:(I)Ljava/lang/String;
        19: invokevirtual #39                 // Method scala/Option$.apply:(Ljava/lang/Object;)Lscala/Option;
        22: new           #41                 // class $anonfun$1
        25: dup
        26: invokespecial #42                 // Method $anonfun$1."<init>":()V
        29: invokevirtual #48                 // Method scala/Option.filterNot:(Lscala/Function1;)Lscala/Option;
        32: new           #50                 // class $anonfun$2
        35: dup
        36: invokespecial #51                 // Method $anonfun$2."<init>":()V
        39: invokevirtual #55                 // Method scala/Option.getOrElse:(Lscala/Function0;)Ljava/lang/Object;
        42: checkcast     #57                 // class java/lang/String
        45: putfield      #17                 // Field res6:Ljava/lang/String;

The pattern match is generally just an if-else, smaller and faster (even considering that it doesn't optimise s == "" to s.isEmpty):

scala> :javap -r #q
  public java.lang.String q(int);
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=5, args_size=2
         0: getstatic     #19                 // Field $line3/$read$$iw$$iw$.MODULE$:L$line3/$read$$iw$$iw$;
         3: iload_1
         4: invokevirtual #22                 // Method $line3/$read$$iw$$iw$.s:(I)Ljava/lang/String;
         7: astore_3
         8: ldc           #24                 // String
        10: aload_3
        11: invokevirtual #28                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
        14: ifeq          22
        17: iconst_1
        18: istore_2
        19: goto          33
        22: aload_3
        23: ifnonnull     31
        26: iconst_1
        27: istore_2
        28: goto          33
        31: iconst_0
        32: istore_2
        33: iload_2
        34: ifeq          44
        37: ldc           #30                 // String <empty>
        39: astore        4
        41: goto          47
        44: aload_3
        45: astore        4
        47: aload         4
        49: areturn

But inspired by the other answer, even if I would never take this code home to meet my parents (because it incorrectly converts the value "null" if the expensive function returns it -- though maybe it's a feature to do that), here is a regex:

scala> def p(i: Int) = "" + s(i) replaceAll ("^null$|^$", "<empty>")
p: (i: Int)String

The "" + s(i) is a shorthand for String.valueOf, which of course produces the String "null" for a null reference value. I appreciate SO's ability not only to generate quick answers to questions, but to encourage some out-of-the-box thinking.