Question about getOrElse from last meeting

48 kali dilihat
Langsung ke pesan pertama yang belum dibaca

Scott Bauer

belum dibaca,
4 Mar 2013, 11.34.0504/03/13
kepadadallas-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

belum dibaca,
4 Mar 2013, 16.03.2904/03/13
kepadadallas-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
Balas ke semua
Balas ke penulis
Teruskan
0 pesan baru