Question about getOrElse from last meeting

48 views
Skip to first unread message

Scott Bauer

unread,
Mar 4, 2013, 11:34:05 AM3/4/13
to dallas-scala...@googlegroups.com
I recall there being some controversy over the use of getOrElse at the last meeting, something about whether or not you could throw an exception during its invocation.  I don't remember the outcome of the (short) conversation, but I ran across this blog post and figured someone might get a brain wrinkle from it: http://whileonefork.blogspot.com/2011/05/magic-of-getorelse.html.  Since getOrElse accepts an expression, you can do anything you need when it gets executed on None.

-Scott

Casey Lucas

unread,
Mar 4, 2013, 4:03:29 PM3/4/13
to dallas-scala...@googlegroups.com
Yea, I mentioned that we use it frequently.  It's good for mapping missing data scenarios to well defined exceptions (when you need that):

val cust : Customer = service.findCustomerByName(name).getOrElse(throw new CustomerNotFoundException());

Or, you can log something before throwing an exception, etc.

-casey
Reply all
Reply to author
Forward
0 new messages