elm-syntax
elm-syntax copied to clipboard
Provide a ModuleName.toString function
I very often need to get a module name as a string, to report it to the user in a human readable format, or to compare it to the configuration that a user gave (I don't want to have to let them give it to me as the underlying type, they shouldn't have to know that).
So I have written String.join "." moduleName
dozens of times in a lot of rules and elm-review
code, sometimes defining it in a new function, sometimes not.
In v8, the ModuleName will be made available as a ( String, List String )
(which by the way, is not reflected in the ModuleName
module) or a SomeVariant String (List String)
.
With that change, the code need to turn a module name into a string becomes more complex: String.join "." ( first :: rest )
which becomes quite annoying.
Proposal
Provide a ModuleName.toString function:
module ModuleName exposing (...)
toString : ( String, List String) -> String
I think the signature is better as ( String, List String) -> String
than as String -> List String -> String
because it is easier to do
case foo of
SomeVariant first rest _ ->
ModuleName.toString ( first, rest )
than to do the following, where you necessarily need the destructuring step:
let ( first, rest ) = getModuleNameFromContext something
in ModuleName.toString first rest
Side-notes / side-proposal
I think that ModuleName.ModuleName
should reflect the new format of a module name that will come in v8.
Also, as I mentioned before, it would be helpful to provide a way to validate that a string corresponds to a module name, meaning that it would be nice for a user to be able to "parse" a module name
module ModuleName exposing (...)
fromString : String -> Maybe ( String, List String ) -- Or maybe a Result
Without something like this, I think that most people will only validate a module name by splitting the module name by "."
or something or to create a crude regex, but that wouldn't necessarily match elm-syntax
's implementation.
I thought the two proposals were kind of related, but I can split this into two issues if you prefer.
Is it actually helpful to represent the module name as its dot-separated parts instead of a (non-empty) String
?
I'm not sure if it should be ( String, List String )
or ( List String, String )
. ( String, List String )
makes it easier to take [ "TopLevel", "Subpath", "MyModule" ]
and just do List.head to get ( String, List String )
, but it seems weird to place emphasis to place emphasis on "TopLevel"
. ( List String, String )
makes more sense in that regard because with ( [ "TopLevel", "Subpath" ], "MyModule" )
the actual module name is separated from the module path.