elm-format
elm-format copied to clipboard
Multiline function arguments
I have a function that takes approximately a billion arguments, which makes the line a bit too long for my taste. Should there be an option to have multiline arguments to a function?
E.g., this
toChange
id
changeType
userName
bot
wiki
serverName
title
comment
serverUrl
serverScriptPath
revision
=
instead of this
toChange id changeType userName bot wiki serverName title comment serverUrl serverScriptPath revision =
P.S. I'm not sure where the = would be most appropriate.
P.P.S. Yeah, my code is probably horrible. If anyone wants to improve it, contact me 😇
use record(s) ? :-)
@rundis I'm not 100% sure how I'd do that; this is a result of using Json.Decode.Pipeline. Regardless, is it as much of an antipattern as to not warrant a way to format? (Genuine question; I'm new to all this.
This is also valid for fewer arguments that have a long length.
This has come up again, and I don't necessarily want to be one to bump my own issues, but since long type signatures can now be split into multiple lines, I'd assume the same should theoretically be possible for parameters as well?
I run into this when passing in parts of the Model to a view function, so I guess I could pass in the entire model record, but that would make it harder to see what is actually used by the function, and feels like bad practice.
I would very much like this, and also sort record fields alphabetically in both type declarations and when destructuring in function parameters.
I would also very much like this.
I totally agree with the people saying "Just use records" and "have fewer arguments". My style of writing Elm code seems heavy in favor of records as arguments and fewer arguments.
But there are times when you just cant. In those cases, its not I just dont have the desire, its that its just very difficult or impossible. Here are some cases:
Url parsing.
Urls might have many many many parameters. At my current job, and at my previous job, we would have to use elm/url to parse >10 possible parameters from a url. The api for such a url parser simply is a function that takes 10 arguments. Maybe there is a way to get around this. I have tried (without success) doing some kind of recursive pipeline, but so far I havent figured it out.
Decoding and pipeline things
A record name can act as a function in decoding, and we take advantage of this for pipeline approaches to decoding json.
Decode.succeed Model
|> Pipeline.required "a" Decode.int
|> Pipeline.required "b" Decode.string
|> Pipeline.required "c" complexDecoder
In my experience this can get complicated when there are many fields to a Model, and the fields are a mix between values fetched via http, passed in values, and default values. Handling this complexity usually means resorting to a big lambda at the top.
Decode.suceed (\a b c d e f -> )
Record structuring
If you use a record as an argument, and you choose to de-structure it, you run into the same problem anyway. Elm-format will not preserve this formatting if you type it.
f { a
, b
, c
} =
I usually just choose not to de-structure to avoid this, and that seems mostly fine, but it is a fact that if you have many parameters you have a hard trade off between horizontally-short lines and destructuring.