F# in Action: A Revised Edition of Get Programming with F# by Isaac Abraham

F# in Action: A Revised Edition of Get Programming with F# by Isaac Abraham

Author:Isaac Abraham [Abraham, Isaac]
Language: eng
Format: epub, mobi
Publisher: Manning Publications Co.
Published: 2024-04-15T22:00:00+00:00


Figure 9.1 Unlike an int, an int option can have two outcomes: either it wraps a real int, or it has nothing in it. You need to pattern match on the value to safely check whether there’s data inside it and then proceed accordingly.

Another crucial thing to note is as everything in F# is an expression, you have to handle both the Some and None cases. In other words, you can’t just deal with the “happy path.” While this might sound like extra work, think about it: if you’ve modeled your domain to say that a given value might be absent, why wouldn’t you want to handle that?

Getting the value out of an option

There is a .Value member of the option type that allows you to get the underlying data without needing to pattern match first. If the value doesn’t exist, you’ll get a runtime exception. Don’t ever use this in a real application, no matter how sure you are that the value will never be None. You’re essentially turning off the type system and losing all the safety of option. It is, however, sometimes useful as an escape hatch if you’re working in scripts in a data exploration mode and want to quickly get at some data you know is there.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.