# Tooling: can one "unwrap" a function?

**URL:** <https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007>\
**Category:** Learn\
**Created:** [February 26, 2021, 7:10am UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007 "2021-02-26T07:10:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjamin-thomas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/benjamin-thomas/32/3869_2.png) [@benjamin-thomas](https://discourse.elm-lang.org/u/benjamin-thomas)\
**Post date:** [February 26, 2021, 7:10am UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/1 "2021-02-26T07:10:48Z")

</div>

Hello I’m following along with the guides…

Please have a look at [this gist](https://gist.github.com/benjamin-thomas/c956b448dd4987466a4e886fd556f45c#file-forms-elm-L129)

After fiddling and trying to find a working solution, I decided it’s time to introduce a default `model.name` on page load.

However, I choose the wrong abstraction on line #129. So I must “revert” the 2 levels of indirection I introduced. That way, I can inject the necessary `value model.name` on line #130.

Choosing the wrong abstraction is quite common when coding IMO.

So I can only suppose this situation is quite common in Elm too, and it must be a bit of pain trying to “unwrap” functions similar to this one later on once your project evolved quite a bit.

So I’m wondering: are there tools that exists and can unwrap this way?

This:

```elm
textInput "Your name" SetName

```

Transformed into:

```elm
input_ "text" "Your name" SetName

```

Transformed into:

```elm
input [type_ "text", placeholder "Your name", onInput SetName] []

```

I can understand that all the information is there statically.

Sorry if this is a noobish question, I’m not quite sure what I’m looking for.

I’m using [this vscode exension](https://marketplace.visualstudio.com/items?itemName=Elmtooling.elm-ls-vscode) btw

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![ChrisWellsWood](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/chriswellswood/32/5293_2.png) [@ChrisWellsWood](https://discourse.elm-lang.org/u/ChrisWellsWood)\
**Post date:** [February 26, 2021, 9:09am UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/2 "2021-02-26T09:09:15Z")

</div>

As far as I’m aware, it’s not possible to “unwrap” a curried function (I think that’s what you’re asking). In practice, I don’t think that this is a massive issue, mainly as refactoring is very easy in Elm. If you do something like this, I’d just change the function to something like:

```elm
textInput : { placeholder : String, value : String, inputMsg : String -> Msg } -> Html Msg

```

Then just follow the compiler errors until it’s refactored.

---

<div class="post-metadata">

**Author:** ![Janiczek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/janiczek/32/4516_2.png) [@Janiczek](https://discourse.elm-lang.org/u/Janiczek)\
**Post date:** [February 26, 2021, 9:30am UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/3 "2021-02-26T09:30:57Z")

</div>

We sometimes run into this situation in our Elm codebase, and given Elm’s compiler behind our backs and helpful messages, it’s indeed enough to follow the compiler errors. It’s not daunting / painful at all but just a bit mechanical (depends on the number of usages of that function you have). That’s where Vim-fu comes up 😄

---

<div class="post-metadata">

**Author:** ![allanderek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/allanderek/32/1360_2.png) [@allanderek](https://discourse.elm-lang.org/u/allanderek)\
**Post date:** [February 26, 2021, 9:56am UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/4 "2021-02-26T09:56:32Z")

</div>

**tl;dr** Sorry OP I do not think what you’re after is available, but it’s a good question, maybe some IDE that I do not use (such as the intelliJ Elm plug-in which I’m told is excellent) has this feature, but I don’t know of any.

I think the respondents here have misunderstood the question. Of course maybe it’s me who is misunderstanding. I believe the OP is talking about a situation in which you have developed a ‘convenience’ function. As he has done a `textInput` function:

```elm
textInput : String -> (String -> Msg) -> Html Msg
textInput currentValue onInput =
      Html.input
            [ Attributes.type_ "text"
            , Attributes.value currentValue
            , Events.onInput onInput
            ]
            []       

```

So it’s just a little convenience function, maybe you have a bunch of text inputs so you have something like:

```elm
Html.form
     [Events.onSubmit ....]
     [ textInput model.name NameInput
     , textInput model.email EmailInput
     , textInput model.petsName PetsNameInput
     , ... etc
     ]

```

Now you realise, that you want the _email_ input to have input type `email` rather than text, and you want it to have a placeholder. So all he wants is an editor command that would simply inline the current function, so that the above would be replaced with:

```elm
Html.form
     [Events.onSubmit ....]
     [ textInput model.name NameInput
     , Html.input
            [ Attributes.type_ "text"
            , Attributes.value model.email
            , Events.onInput EmailInput
            ]
            []       
     , textInput model.petsName PetsNameInput
     , ... etc
     ]

```

So that he can then edit the `Html.input` and change the input type and add a placeholder attribute. So the word you were looking for was _“inlining”_ as opposed to _“unwrap”_.

So he’s basically after refactoring support from the editor. I don’t believe this particular support yet exists, but it’s a good question, this does come up.

---

<div class="post-metadata">

**Author:** ![opvasger](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@opvasger](https://discourse.elm-lang.org/u/opvasger)\
**Post date:** [February 26, 2021, 3:55pm UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/5 "2021-02-26T15:55:34Z")

</div>

> [@benjamin-thomas](#):
>
> are there tools that exists and can unwrap this way?

not as far as I know, but it could be a lot of fun to hack on a tool for it (and maybe more?).

Perhaps using Elm itself, [this parser-module](https://package.elm-lang.org/packages/stil4m/elm-syntax/7.2.1/), and a Node.js CLI-wrapper, to quickly get started:

1. Parse some modules
2. Perform some rewrite-rules
3. Dump it back into some files

Let me know if you’d like to try something along these lines ☀

---

<div class="post-metadata">

**Author:** ![benjamin-thomas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/benjamin-thomas/32/3869_2.png) [@benjamin-thomas](https://discourse.elm-lang.org/u/benjamin-thomas)\
**Post date:** [February 26, 2021, 5:37pm UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/6 "2021-02-26T17:37:17Z")

</div>

Thanks all 😉

Of course when I was talking about “pain” I was exaggerating : the tooling is already very nice. In parallel, I am trying the intellij plugin too and it it is indeed very nice, less noisy than the vscode one.

@Janiczek I’m a vimmer too don’t worry, I guess I was having a full on lazy moment ^^

@allanderek that’s right, “inlining” feels like the right term. I’d imagine an editor command that would “inline” the code one level up (or is it down? 🙃) upon each keypress.

opvasger this look like a very interesting package. I think this language is clicking with me so I’ll stick to digging into it more for now but I’ll definitely keep this in mind for later thanks.

---

<div class="post-metadata">

**Author:** ![razze](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/razze/32/1650_2.png) [@razze](https://discourse.elm-lang.org/u/razze)\
**Post date:** [March 2, 2021, 9:10pm UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/7 "2021-03-02T21:10:16Z")

</div>

Should be fairly simple in the language server, if somebody want’s to address this.

I can mentor.

---

<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:** [March 12, 2021, 9:10pm UTC](https://discourse.elm-lang.org/t/tooling-can-one-unwrap-a-function/7007/8 "2021-03-12T21:10:57Z")

</div>

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