elm-repl icon indicating copy to clipboard operation
elm-repl copied to clipboard

Unable to destructure tuples?

Open tomduncalf opened this issue 10 years ago • 11 comments

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?

tomduncalf avatar Jul 26 '15 16:07 tomduncalf

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 avatar Jan 07 '16 16:01 co-dh

@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.

jvoigtlaender avatar Feb 04 '16 12:02 jvoigtlaender

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?

    %
    *
    +
    -

rtfeldman avatar Mar 09 '16 22:03 rtfeldman

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.)

rtfeldman avatar Mar 09 '16 22:03 rtfeldman

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

kkruups avatar Aug 31 '16 06:08 kkruups

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 😃

hatashiro avatar Sep 05 '16 08:09 hatashiro

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.

rdrey avatar Oct 25 '16 01:10 rdrey

@noraesae is anyone actively looking into this?

jochasinga avatar Dec 30 '16 07:12 jochasinga

I am not who was asked the question, but I am pretty sure the answer is no.

jvoigtlaender avatar Dec 30 '16 07:12 jvoigtlaender

Can we get a fix for this?

BR/

kkruups avatar Jan 31 '17 06:01 kkruups

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

kkruups avatar Feb 01 '17 03:02 kkruups