how to do this in scala?
You can write it the same way, or you could use Option:
(Option(s) match {
case Full(s) if (!s.isEmpty) => String filled
case _ => String was null
}
if (!Option(s).getOrElse("").isEmpty)
Basically, you're wrapping a value that potentially could be null into an Option so you don't have to specifically test against null.
For one single statement it may seem like more work, but it's more like a style of writing code, not having to explicitly guard against null but passing an Option[YourType] around…
Torsten.
Hi,what about a good old StringUtils.isEmpty(s) from Apache Commons ??
I suspect a large number of people on this list would strongly suggest
that rather than checking preconditions, you should leverage the type
system to enforce those conditions without (fallible) programmer
intervention.
Well – if you have to do this quite a bit, get into a habit and define yourself a nice utility method (via pimping):
def nz : Boolean = s != null && s.length > 0 // the way of the Java
def nzo : Option[String] = if (nz) Some(s) else None // the way of the lambda
}
implicit def tonzo (s:String) = new NZOString (s)
// sample:
var s : String = null
var r = 1
if (s.nz) r += 1 else r -= 1
s.nzo map (s=>r+=1)
r
How can you call methods on a possible null? As long as the compiler knows the type, it will go via NZOString(s). for instance, this will bomb:
scala> null.nz
<console>:10: error: value nz is not a member of Null
^
scala> s=null
s: String = null
scala> s.nz
res5: Boolean = false
scala> i
> I'd do it exactly the same as in Java. All of the alternatives I have seen
> in this thread so far are (1) longer and (2) slower. But then I'd try to
> deal with nulls at the interfaces to Java, so that I can forget about null
> inside my Scala code.
I'm currently writing some Scala that interfaces with the JavaMail
library, and JavaMail loves to return you nulls all over the place, uses
lots of raw types and returns objects that you have to upcast to do
anything sensible with.
I've just used 'XXX == null' as in Java for the nulls, the one that
caused a bit of head-scratching was a method that returned a raw Java
Enumeration that I wanted to use as a Scala enumeration. I ended up with:
import java.util.{Enumeration => JEnum}
import scala.collection.JavaConverters._
msg.getMatchingHeaders(headers).asInstanceOf[JEnum[Header]].asScala.foreach(...)
Which is a bit long-winded but does mean that even pretty wizened Java
APIs can be prettified for use in Scala.
--
Alan Burlison
--
--
Alan Burlison
--
I would say that you are right in the general case, but in this particular case, for Strings, this expression is so common in integrating with the million Java libraries out there, that we could do a lot worse than adding nz and nzo to scala.runtime.RichString…
Cheers,
Razie
From: scala...@googlegroups.com [mailto:scala...@googlegroups.com] On Behalf Of martin odersky
Sent: November-29-11 10:06 AM
To: wuhaixing
Cc: scala-user
Subject: Re: [scala-user] How to test String is null or empty?
On Tue, Nov 29, 2011 at 8:28 AM, wuhaixing <wuha...@gmail.com> wrote:
Hi,
I do not think so.Apache Commons is useless only if you do not have the habit of defensive programming.Apache Commons is for people that knows that we should check preconditions. If you don't then YOU are the problem not an humble jar in your classpath that wants only to help you in avoiding to waste time writing and writing again repetitive code.Yeah, I know, is written in Java (horrible legacy code that must be rewritten in Scala ASAP as the rest of the Universe, I know) but works nicely.Cheers,Ugo.
should be good
- Yo Eight
the question was whether you can treat null and "" identically.
just don't use null....