inline conditional in scala

4,614 views
Skip to first unread message

alexmnyc

unread,
May 21, 2013, 12:35:12 PM5/21/13
to lif...@googlegroups.com
Guys, a quick Scala question.
 
What's the equivalent of ? or ?? opertor in Scala as in:
 
val t = "test".length==4 ? "test is of length 4" : "test i not of length 4";
 
val t = string ?? "string is null"
 
 

Kristof Jozsa

unread,
May 21, 2013, 12:45:01 PM5/21/13
to lif...@googlegroups.com
How about:
val t = if ("test".length == 4) "test is of length 4" else "test i not of length 4"

and
Option(string).getOrElse("string is null")

cheers,
K



 
 

--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
 
---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mikhail Strebkov

unread,
May 21, 2013, 12:46:22 PM5/21/13
to lif...@googlegroups.com
1) val t = if ("test".length == 4) "test is of length 4" else "test i not of length 4"
2) for nullable things in scala there is a Option[T] class, so the closest will be:
val fullString = Some("full")
val emptyString = None
val full = fullString.getOrElse("empty")
val empty = emptyString.getOrElse("empty")

P.S. This group probably is not suited to discuss general basic scala question

alexmnyc

unread,
May 21, 2013, 12:49:47 PM5/21/13
to lif...@googlegroups.com
Thanks guys. I was trying to avoid "if". 
 
I thought maybe I was missing an operator as using "if" felt a little iffy for a functional language somehow so I thought "?" operator was just named into something else and I just could not find in the API docs.
 
Thanks for confirming that "if" is "if" and it is here to stay -)

Reuben Doetsch

unread,
May 21, 2013, 1:09:33 PM5/21/13
to lif...@googlegroups.com
In scalaz there are conditional operators so you can do


import scalaz._
import Scalaz._

 ("test".length == 4) ? "test is of length 4" | "test i not of length 4" 

Andreas Joseph Krogh

unread,
May 21, 2013, 1:11:09 PM5/21/13
to lif...@googlegroups.com
Lift comes with its own ternary operator, and it's used like this:
 
import net.liftweb.util.Helpers._
 
(1 == 2) ? "a" | "b"
 
Note that it uses pipe instead of colon as the "else"-branch, because colon is reserved.
 
--
Andreas Joseph Krogh <and...@officenet.no>      mob: +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
Public key: http://home.officenet.no/~andreak/public_key.asc
 

alexmnyc

unread,
May 21, 2013, 2:18:25 PM5/21/13
to lif...@googlegroups.com
Lift helpers looks great. Thank you!

Naftoli Gugenheim

unread,
May 22, 2013, 12:30:57 AM5/22/13
to lif...@googlegroups.com


On Tuesday, May 21, 2013, alexmnyc wrote:
Thanks guys. I was trying to avoid "if". 
 
I thought maybe I was missing an operator as using "if" felt a little iffy for a functional language somehow so I thought "?" operator was just named into something else and I just could not find in the API docs.

Yes it was renamed to "if." :) Scala was designed to be more consistent than Java, so they unified the "if statement" and the ternary operator, so that there is only the "if expression".

 
Thanks for confirming that "if" is "if" and it is here to stay -)

--
Reply all
Reply to author
Forward
0 new messages