How to substitute an empty string (or null) with a default string concisely in Scala -


i have method returns string. want substitute default value such "<empty>" if returns empty string or null. let's assume name getsomestring, expensive operation can call once, , can't change return type option[string]. now, i'm doing following:

val mystr = {   val s = getsomestring   if (s == null || s.isempty) "<empty>" else s } 

is there simpler way achieve same thing?

given expensive function:

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

i think easy read , free of overhead, cf this in 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 eyes, not expressive, minimalist punctuation:

scala> option(s(0)) filternot (_.isempty) getorelse "<empty>" res6: string = <empty> 

moreover, contrast cost in anonfun classes closures , additional method invocations:

scala> :javap -   size 1161 bytes   md5 checksum 765f5f67b0c574252b059c8adfab1cf0   compiled "<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 if-else, smaller , faster (even considering doesn't optimise s == "" 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 other answer, if never take code home meet parents (because incorrectly converts value "null" if expensive function returns -- though maybe it's feature that), here regex:

scala> def p(i: int) = "" + s(i) replaceall ("^null$|^$", "<empty>") p: (i: int)string 

the "" + s(i) shorthand string.valueof, of course produces string "null" null reference value. appreciate so's ability not generate quick answers questions, encourage out-of-the-box thinking.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -