2

I'm admittedly very new to Scala, and I'm having trouble with the syntactical sugar I see in many Scala examples. It often results in a very concise statement, but honestly so far (for me) a bit unreadable.

So I wish to take a typical use of the Option class, safe-dereferencing, as a good place to start for understanding, for example, the use of the underscore in a particular example I've seen.

I found a really nice article showing examples of the use of Option to avoid the case of null.

https://medium.com/@sinisalouc/demystifying-the-monad-in-scala-cc716bb6f534#.fhrljf7nl

He describes a use as so:

trait User {
  val child: Option[User]
}

By the way, you can also write those functions as in-place lambda functions instead of defining them a priori. Then the code becomes this:

val result = UserService.loadUser("mike")
  .flatMap(user => user.child)
  .flatMap(user => user.child)

That looks great! Maybe not as concise as one can do in groovy, but not bad.

So I thought I'd try to apply it to a case I am trying to solve.

I have a type Person where the existence of a Person is optional, but if we have a person, his attributes are guaranteed. For that reason, there are no use of the Option type within the Person type itself.

The Person has an PID which is of type Id. The Id type consists of two String types; the Id-Type and the Id-Value.

I've used the Scala console to test the following:

class Id(val idCode : String, val idVal : String)

class Person(val pid : Id, val name : String)

val anId: Id = new Id("Passport_number", "12345")

val person: Person = new Person(anId, "Sean")

val operson : Option[Person] = Some(person)

OK. That setup my person and it's optional instance.

I learned from the above linked article that I could get the Persons Id-Val by using flatMap; Like this:

val result = operson.flatMap(person => Some(person.pid)).flatMap(pid => Some(pid.idVal)).getOrElse("NoValue")

Great! That works. And if I infact have no person, my result is "NoValue".

I used flatMap (and not Map) because, unless I misunderstand (and my tests with Map were incorrect) if I use Map I have to provide an alternate or default Person instance. That I didn't want to have to do.

OK, so, flatMap is the way to go.

However, that is really not a very concise statement. If I were writing that in more of a groovy style, I guess i'd be able to do something like this:

val result = person?.pid.idVal

Wow, that's a bit nicer!

Surely Scala has a means to provide something at least nearly as nice as Groovy?

In the above linked example, he was able to make his statement more concise using some of that syntactical sugar I mentioned before. The underscore:

or even more concise:

val result = UserService.loadUser("mike")
  .flatMap(_.child)
  .flatMap(_.child)

So, it seems in this case the underscore character allows you to skip specifying the type (as the type is inferred) and replace it with underscore.

However, when I try the same thing with my example:

val result = operson.flatMap(Some(_.pid)).flatMap(Some(_.idVal)).getOrElse("NoValue")

Scala complains.

<console>:15: error: missing parameter type for expanded function ((x$2) => x$2.idVal)
       val result = operson.flatMap(Some(_.pid)).flatMap(Some(_.idVal)).getOrElse("NoValue")

Can someone help me along here?

How am I misunderstanding this? Is there a short-hand method of writing my above lengthy statement? Is flatMap the best way to achieve what I am after? Or is there a better more concise and/or readable way to do it ?

thanks in advance!

1

2 Answers 2

10

Why do you insist on using flatMap? I'd just use map for your example instead:

val result = operson.map(_.pid).map(_.idVal).getOrElse("NoValue")

or even shorter:

val result = operson.map(_.pid.idVal).getOrElse("NoValue")

You should only use flatMap with functions that return Options. Your pid and idVals are not Options, so just map them instead.

You said

I have a type Person where the existence of a Person is optional, but if we have a person, his attributes are guaranteed. For that reason, there are no use of the Option type within the Person type itself.

This is the essential difference between your example and the User example. In the User example, both the existence of a User instance, and its child field are options. This is why, to get a child, you need to flatMap. However, since in your example, only the existence of a Person is not guaranteed, after you've retrieved an Option[Person], you can safely map to any of its fields.

Think of flatMap as a map, followed by a flatten (hence its name). If I mapped on child:

val ouser = Some(new User())
val child: Option[Option[User]] = ouser.map(_.child)

I would end up with an Option[Option[User]]. I need to flatten that to a single Option level, that's why I use flatMap in the first place.

1
  • Ah ok, thanks! I messed up my earlier attempts at map. And thanks for the explanation!
    – svaens
    Feb 8, 2016 at 11:33
7

If you looking for the most concise solution, consider this:

val result = operson.fold("NoValue")(_.pid.idVal)

Though one could find it not clear or confusing

1
  • Great! Thanks for the alternative solution. I'll look into that.
    – svaens
    Feb 8, 2016 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.