# Is there an equivalent of Haskell's fromMaybe?

**URL:** https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367
**Category:** New to Julia
**Tags:** question
**Created:** [July 27, 2021, 1:20pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367 "2021-07-27T13:20:02Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![orome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orome/32/26965_2.png) [@orome](https://discourse.julialang.org/u/orome)
#### Post date: [July 27, 2021, 1:20pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/1 "2021-07-27T13:20:02Z")

</div>

Is there a Julia equivalent of (or well worn idiom for) Haskell’s [`fromMaybe`](https://hoogle.haskell.org/?hoogle=fromMaybe), e.g., for use with `Union{T, Nothing}`?

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [July 27, 2021, 1:24pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/2 "2021-07-27T13:24:28Z")

</div>

`something` seems to be it:

```julia
help?> something
search: something @something

  something(x...)

  Return the first value in the arguments which is not equal to nothing, if any. Otherwise throw an error. Arguments of type Some are
  unwrapped.

```

---

<div class="post-metadata">

### Author: ![orome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orome/32/26965_2.png) [@orome](https://discourse.julialang.org/u/orome)
#### Post date: [July 27, 2021, 2:21pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/3 "2021-07-27T14:21:47Z")

</div>

Is it the case though that `something(x, y, ...)` evaluates `y` even if `x` is not `nothing`?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 27, 2021, 2:26pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/4 "2021-07-27T14:26:29Z")

</div>

`something` is a function like any other; Julia is not lazy like Haskell. Arguments are evaluated first.

(types of arguments are also not part of the type of a function)

---

<div class="post-metadata">

### Author: ![orome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orome/32/26965_2.png) [@orome](https://discourse.julialang.org/u/orome)
#### Post date: [July 27, 2021, 2:28pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/5 "2021-07-27T14:28:13Z")

</div>

Yeah sorry that was a dumb question. I hadn’t realized that. (But ugh!)

---

<div class="post-metadata">

### Author: ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)
#### Post date: [July 27, 2021, 2:28pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/6 "2021-07-27T14:28:19Z")

</div>

There’s the macro `@something` for that.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 27, 2021, 2:30pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/7 "2021-07-27T14:30:19Z")

</div>

Note that `@something` is an AST transform (commonly known as a macro), not a function:

```julia
julia> @macroexpand @something a b    
:(something(let val                   
          #= some.jl:143 =#           
          if (val = a) !== nothing    
              val                     
          else                        
              if (val = b) !== nothing
                  val                 
              else                    
                  nothing             
              end                     
          end                         
      end))                           

```

This works because all expressions return their last value (in this case `a`, `b` or `nothing` as a fallback).

---

<div class="post-metadata">

### Author: ![orome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orome/32/26965_2.png) [@orome](https://discourse.julialang.org/u/orome)
#### Post date: [July 27, 2021, 2:33pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/8 "2021-07-27T14:33:35Z")

</div>

Is `Maybe.jl` widely used? I’m new and don’t want to gum things up trying to make Julia something (see what I did there) that it isn’t. Better to deal with its idioms.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 27, 2021, 2:35pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/9 "2021-07-27T14:35:42Z")

</div>

[According to JuliaHub](https://juliahub.com/ui/Packages/Maybe/pXrXu/0.1.5?t=2), `Maybe.jl` has 0 Dependents, so I’d say no.

Usually, `nothing` and `missing` values are handled immediately instead of deferred to a later point in time. Maybe you can tell us more about what you want to code up and we can suggest some way of achieving it?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 27, 2021, 2:39pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/10 "2021-07-27T14:39:03Z")

</div>

`@something` was merged into Base. So it’s definitely idiomatic.

---

<div class="post-metadata">

### Author: ![orome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orome/32/26965_2.png) [@orome](https://discourse.julialang.org/u/orome)
#### Post date: [July 27, 2021, 2:44pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/11 "2021-07-27T14:44:09Z")

</div>

I can’t figure out how to invoke `@something`. I’ve tried `@something(...)` and `Base.@something(...)`.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 27, 2021, 2:47pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/12 "2021-07-27T14:47:29Z")

</div>

Just like in the `@macroexpand` call I’ve used above:

```julia
julia> a = 1                                    
1                                               
                                                
julia> b = nothing                              
                                                
julia> @something a b                           
1                                               
                                                
julia> @something b a                           
1                                               
                                                
julia> @something b b                           
ERROR: ArgumentError: No value arguments present
Stacktrace:                                     
 [1] something()                                
   @ Base ./some.jl:99                          
 [2] something(::Nothing)                       
   @ Base ./some.jl:100                         
 [3] top-level scope                            
   @ some.jl:143                                

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 27, 2021, 2:48pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/13 "2021-07-27T14:48:14Z")

</div>

Ah my bad! `@something` was a very recent addition, and is not in the LTS version (or 1.6 I think).

You are right that `something(...)` evaluates it’s arguments.

To emulate `@something`, i.e. have `fromMaybe` which isn’t eager, you can use the `if...else` pattern shown above.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 27, 2021, 2:49pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/14 "2021-07-27T14:49:00Z")

</div>

Yes, the `@something` macro will be available from 1.7 onwards.

---

<div class="post-metadata">

### Author: ![fonsp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fonsp/32/222349_2.png) [@fonsp](https://discourse.julialang.org/u/fonsp)
#### Post date: [July 27, 2021, 2:52pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/15 "2021-07-27T14:52:08Z")

</div>

Use the `something` function in combination with the `Some` type.

---

<div class="post-metadata">

### Author: ![orome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orome/32/26965_2.png) [@orome](https://discourse.julialang.org/u/orome)
#### Post date: [July 27, 2021, 2:57pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/16 "2021-07-27T14:57:36Z")

</div>

Right now, just test driving the language. To actually do what I want to do it’s probably simpler (and clearer) to just do something like:

```julia
    if haskey(things, n)
        return things[n]
    else
       return a_new_thing
    end

```

rather than

```julia
    @something( get(things, n, nothing), a_new_thing )

```

In practice making `a_new_thing` has some setup so the former makes sense anyway unless I use `let`.

For reference the equivalent Haskell that inspired the question is something like

```nohighlight
get_thing n = fromMaybe (Thing n part_1 part_2) (M.lookup n things)
    where
        part_1 = a_bunch_of_stuff_that_doest_fit_above
        part_2 = more_stuff_that_doest_fit_above

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 27, 2021, 2:59pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/17 "2021-07-27T14:59:41Z")

</div>

Sounds like you want to skip the `nothing` and use `get!` or `get` directly:

```julia
 get!(f::Function, collection, key)                                                                                   
                                                                                                                      
 Return the value stored for the given key, or if no mapping for the key is present, store key => f(), and return f().

```

Usage:

```julia
julia> d = Dict()                                         
Dict{Any, Any}()                                          
                                                          
julia> get!(() -> begin println("hello"); 1 end, d, "key")
hello                                                     
1                                                         
                                                          
julia> get!(() -> begin println("hello"); 1 end, d, "key")
1                                                         
                                                          
julia> d                                                  
Dict{Any, Any} with 1 entry:                              
  "key" => 1                                              

```

By the way, this kind of docstring can be easily accessed by using the REPL help mode, invoked by entering `?` at the REPL prompt.

---

<div class="post-metadata">

### Author: ![orome](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orome/32/26965_2.png) [@orome](https://discourse.julialang.org/u/orome)
#### Post date: [July 27, 2021, 3:26pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/18 "2021-07-27T15:26:27Z")

</div>

That’s good, except I don’t want to store the generated one.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [July 27, 2021, 3:34pm UTC](https://discourse.julialang.org/t/is-there-an-equivalent-of-haskells-frommaybe/65367/19 "2021-07-27T15:34:13Z")

</div>

You can do that with `get` (no exclamation mark):

```julia
julia> d = Dict()                                        
Dict{Any, Any}()                                         
                                                         
julia> get(() -> begin println("hello"); 1 end, d, "key")
hello                                                    
1                                                        
                                                         
julia> get(() -> begin println("hello"); 1 end, d, "key")
hello                                                    
1                                                        
                                                         
julia> d                                                 
Dict{Any, Any}()                                         
                                                         
julia> d["key"] = 44                                     
44                                                       
                                                         
julia> get(() -> begin println("hello"); 1 end, d, "key")
44                                                       

```

It’s a convention (not enforced by the compiler) that functions that modify one or more of their arguments end in a `!`. There’s no auto generation of such modifying functions, but most of the time when one version exists, the other does as well.
