background preloader

Scala

Facebook Twitter

Scala - modifying nested elements in xml. Understanding Lift’s Box[T] monad « The Loop. Understanding Lift’s Box[T] monad So im at Scala Days 2010 right now, and during dinner last night their seemed to be some misconceptions about what Lift's Box[T] monad actually does and how it differs from Option[T]. Now, im not computer scientist, so im not going to be discussing the algebraic validity of Box[T], not shall I be calling it a "tri state option"... Why bother boxing values? Have you ever thrown a NullPointerException?

Frankly, I don't know any developers that haven't had this happen to them at some point because of something they hadn't considered during development... in short, a NPE is something that was caused by an unexpected series of events causing your application to explode in a variety of ways. Consider a scenario where I want to do something with a value returned from a database; its not uncommon to see programs where one assumes the database query got the correct result, then do some operation on it. ...Boxes to the rescue! Import net.liftweb.common. The ? Scala - liftweb - accessing get/post parameters. Scala Option, Lift Box and how to make your code better - Lift. Scala has a ton of nice features. One of the features that I was slow to adopt, until Burak Emir gently reminded me a bunch of times, is "Options". Read on about Options, Boxes, and how Lift makes good use of the to make clean, error resistant code.

If you come from an imperative (Java, Ruby) background, you'll probably recognize the following code: x = someOperation if ! X.nil? Y = someOtherOperation if ! Y.nil? Okay, so that's pseudo-code, but there are tons of operation, guard, operation, guard, blah blah constructs. Further, null/nil are passed around as failures. In Java, null is a non-object. Ruby has nil which is marginally better than null. nil is a real, singleton object. Scala does something different. There's an abstract class, called Option. Def findUser(name: String): Option[User] = { val query = buildQuery(name) val resultSet = performQuery(query) val retVal = if (resultSet.next) Some(createUser(resultSet)) else None resultSet.close retVal } You can nest map operations:

Real-World Scala: Dependency Injection (DI) How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections.