Tooling: can one "unwrap" a function?

Hello I’m following along with the guides…

Please have a look at this gist

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:

textInput "Your name" SetName

Transformed into:

input_ "text" "Your name" SetName

Transformed into:

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 btw

Thanks :slight_smile:

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:

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

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

2 Likes

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 :grinning_face_with_smiling_eyes:

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:

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:

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:

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.

1 Like

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, 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 :sunny:

Thanks all :wink:

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? :upside_down_face:) 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.

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

I can mentor.

2 Likes

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