# Using Maybe.withDefault as a type

**URL:** <https://discourse.elm-lang.org/t/using-maybe-withdefault-as-a-type/7514>\
**Category:** Learn\
**Created:** [July 4, 2021, 2:30am UTC](https://discourse.elm-lang.org/t/using-maybe-withdefault-as-a-type/7514 "2021-07-04T02:30:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhelbegor](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dhelbegor/32/2745_2.png) [@dhelbegor](https://discourse.elm-lang.org/u/dhelbegor)\
**Post date:** [July 4, 2021, 2:30am UTC](https://discourse.elm-lang.org/t/using-maybe-withdefault-as-a-type/7514/1 "2021-07-04T02:30:46Z")

</div>

Hi guys 😃 , I’m trying to use Elm Maybe withdefault as a type but it is not working correctly as I expected.

What I’m trying to do?  
I’m trying to create a base url that concat 2 parameters and return a complete url as result.

```auto
base: String -> (Maybe.withDefault "" String) -> String
base version path =
    String.concat ["https://127.0.0.1:8000/api/", version, "/models/", path]

```

if the path is provided I need to return the url with the path provided, other wise I’ll just do nothing with the path (e.g: not required parameter ).

i’m getting the error bellow. I’m trying to figure out what I’m doing wrong. can you guys give me a help please?

```auto
./src/Main.elm
Error: Compiler process exited with error Compilation failed
Compiling ...-- PROBLEM IN TYPE ANNOTATION -------------------------------------- src/Api.elm

I was partway through parsing the `base` type annotation, but I got stuck here:

3| base: String -> (Maybe.withDefault "" String) -> String
                   ^
I was expecting to see a type next. Try putting Int or String for now?

```

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [July 4, 2021, 6:38am UTC](https://discourse.elm-lang.org/t/using-maybe-withdefault-as-a-type/7514/2 "2021-07-04T06:38:43Z")

</div>

Hi! You cannot use `Maybe.withDefault` in a type annotation. In general, you can’t call functions in type annotations.

If I understand you correctly, you want to build a URL where the last segment can be optional. If so, one way of modelling that is by using `Maybe String`. Then you can use `Maybe.withDefault ""` to handle `Nothing` – which you seem to be on track towards, but got stuck with _where_ to put that `Maybe.withDefault`:

```elm
base : String -> Maybe String -> String
base version path =
    String.concat ["https://127.0.0.1:8000/api/", version, "/models/", path |> Maybe.withDefault ""]

```

* * *

Some extra thoughts:

You could also go with just a `String`, since you can pass in an empty string to get the same result:

```elm
base : String -> String -> String
base version path =
    String.concat ["https://127.0.0.1:8000/api/", version, "/models/", path]

```

But then it’s not as clear anymore that `path` can intentionally be missing. 🤷‍♂️

When you have multiple parameters with the same type it can be nice to use a record to make mixups harder:

```elm
base : { version: String, path : String } -> String
base { version, path } =
    String.concat ["https://127.0.0.1:8000/api/", version, "/models/", path]

```

You could also use [Url.Builder.absolute](https://elm.dmy.fr/packages/elm/url/latest/Url-Builder#absolute) if you feel fancy.

---

<div class="post-metadata">

**Author:** ![dhelbegor](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dhelbegor/32/2745_2.png) [@dhelbegor](https://discourse.elm-lang.org/u/dhelbegor)\
**Post date:** [July 4, 2021, 3:26pm UTC](https://discourse.elm-lang.org/t/using-maybe-withdefault-as-a-type/7514/3 "2021-07-04T15:26:36Z")

</div>

Thank very much for your explanation 😃 , I did the modifications and it worked great fully. I’ll take a loot at the Url.Builder too 😃 .

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [July 14, 2021, 3:27pm UTC](https://discourse.elm-lang.org/t/using-maybe-withdefault-as-a-type/7514/4 "2021-07-14T15:27:14Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
