elm-repl
elm-repl copied to clipboard
Unable to destructure tuples?
I'm a total elm newbie so I may be way off the mark here, but when trying to destructure a tuple into variables, I get the following error:
> (a, b) = (1, 2)
## ERRORS in repl-temp-000.elm #################################################
-- NAMING ERROR ---------------------------------------------- repl-temp-000.elm
Cannot find variable `$`
4| a$b
^^^
Maybe you want one of the following?
%
*
+
-
Whereas if I do this in actual code (or on http://elm-lang.org/try), it works.
Am I doing something wrong or is this a bug?
let (a, b) = (1, 2) in (a,b) use let expression. repl is not the same as in an elm file where (a,b) =(1,2) will delcare a and b.
@co-dh, however:
> a = 1
will simply work, without having to use let in any way. So it's indeed odd that
> (a,b) = (1,2)
does not work.
Destructuring records also throws an error.
> { foo, bar } = { foo = "blah", bar = 10 }
-- NAMING ERROR ---------------------------------------------- repl-temp-000.elm
Cannot find variable `$`
4│ bar$foo
^^^^^^^
Maybe you want one of the following?
%
*
+
-
I wouldn't be surprised if the problem is that the repl wants to print out the value (with type annotation) that the expression evaluates to, and in this case there are two values instead of one. (Also it's not possible to annotate them, as far as I know.)
The disconnect between repl usage and architecture is extremely frustrating. I understand issue regarding annotations, however, not being able to deconstruct tuples or records in the repl seriously hampers the repl from being useful to survey the language or do any minimal prototyping. Please fix this soon!
Even if you use let as describe above you cannot access the any specific member by its variable name. "Can't find variable" error is indicated when attempting to access the variable.
repl_prompt>coords = ( 3.401, 57.234) repl_prompt>let (x,y) = coords repl_prompt>x //Naming error indicated
Error
-- NAMING ERROR ---------------------
Cannot find variable `x`
4| x
^
Maybe you want one of the following?
e
f
p
Basics.e
In ghci, let is used to define a repl variable. It works like below:
> a = 10 -- fails
> let a = 10 -- no print, set a
> a
10
> let b = 10 in b
10
> b
10
> let (c, d) = (10, 20) -- no print
> d
20
I would suggest making let in elm-repl work the same as it does in ghci, or just allowing (b, c) = (10, 20) with printing (10, 20) to console. If any help is needed, I would be happy to contribute on this issue 😃
Yeah, a fix would be great. I was another confused noob a few minutes ago. :p
If @noraesae's proposal were implemented, I would also like the REPL to issue a nice warning that REPL usage requires a let.
@noraesae is anyone actively looking into this?
I am not who was asked the question, but I am pretty sure the answer is no.
Can we get a fix for this?
BR/
Additional Info:
Inconsistent Behavior depending on how tuple is destructured in elm-repl also not consistent with compiled code outside repl or Online Editor.
( x, y ) = (1,2) fails with error indicated above
( x, _ ) = (1, 2) or ( _, y) = (1, 2) destructures successfully for the value which is not thrown away ,x = 1 and y = 2. (only one value is destructured)
let (a, b) = (1, 2) in (a,b) tuples only destructure successfully within a let.. in statements in repl
btw, elm release v0.18