-
Notifications
You must be signed in to change notification settings - Fork 359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
toString behaves unintuitively #657
Comments
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it! Here is what to expect next, and if anyone wants to comment, keep these things in mind. |
Elm's If you want toStr v =
let
str = toString v
in
if left 1 str == "\"" then
dropRight 1 (dropLeft 1 str)
else
str |
To follow up from Slack - the closest you're going to get to the behaviour you want is (as @pdamoc suggests) stripping the quotes off yourself. A function that could tell you at runtime whether or not a value of type get : Maybe a -> String
get ma = case ma of
Just x -> if isString x
then ???
else toString x
Nothing -> "" How could we fill in the blank? It can't just be If you would like to see some other function of type I still say your function is too general. If a type variable appears once in a function signature, you can't do anything with a value of that type because the compiler doesn't know anything about it. |
This came up in the string interpolation discussion, as I'm curious - is anyone depending on the current behavior, and couldn't work around such a change with |
To be more specific on that last point, in a world where I expect this based on how in JavaScript, |
If you are The behavior of |
I hadn't considered the possibility that interpolation might not actually desugar to a |
@avh4 Thanks for the explanation as to the purpose of I don't buy your assertion that ...in statically typed languages, it is generally not advised to use @rtfeldman, Thanks for pointing out the discussion about interpolation. If/when that is available (I wish it was today!), I would choose that way of representing the value in the view. FWIW, I was lurking on the Slack list a bit last evening, and this very same issue came up from another person on the list (I didn't comment, but had a little chuckle). I think you're definitely working against the Principle of Least Surprise here. |
Looked in the Mailing List and didn't see an issue for this, read through the most promising one about Due to historical context, the name would lead everyone coming to Elm to assume it returns a String representation of the argument, for purposes like interpolation, where What do other languages call it? Given that Haskell calls it What should Elm call it? For the reasons above, I'd advocate |
Another vote for inspect. However, an alternative is the ability to make functions of different types have the same name, that way something like this would be valid if-and-only-if the call path is resolvable from the point it is called at, if the function call is ambiguous (like not having enough arguments to fully determine which should be called) then the compiler should throw an error. This is how many statically typed functional languages work and would work around this issue. Another method would be a function matcher style, where you can have multiple heads of a function and they match on their values and value-types (only if resolvable at compile-time else compiler throws an error), which effectively gives the same ability as the above but is often easier to implement in the compiler while also being more powerful. |
In most popular languages (Java, Javascript, Clojure, Scala to name a few),
toString
simply provides a string representation of a value. However, in Elm it seems to provide a syntax writer (I'm thinking the dual of something like the Clojure reader).There's one edge case where that easily bites: when the value is a
String
already, toString wraps it in quotes:Writing generalised code as I do, I wrote a helper to extract a value from Maybe:
To my surprise, my displayed text was quoted. It seems there's no way in Elm to properly express the desired function above, since you can't tell at runtime if a value is a String.
After a short discussion on Slack #general 4 Jul 2016 1:12am CDT, I posited:
I propose moving or renaming the current toString and providing an implementation that behaves as one would expect coming from other popular languages. Or just give me the ability to tell if a value is a string at runtime, and I can work around this edge case.
Thoughts?
The text was updated successfully, but these errors were encountered: