Skip to content
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

Dict syntax is difficult to remember #2067

Closed
diegozea opened this issue Jan 17, 2013 · 10 comments
Closed

Dict syntax is difficult to remember #2067

diegozea opened this issue Jan 17, 2013 · 10 comments

Comments

@diegozea
Copy link
Contributor

Dict syntax has more than one option, and declaration of types doesn't match with other constructors on Julia.

Maybe the most confusing thing is (KeyType=>ValueType)[...] because doesn't match with Dict{K,V}(n) and others constructors.
Having [...] or {...} is confusing too.

Maybe can be a little more simple, something like:

  Dict{K,V}(n)

  # Using , instead of => on Type declaration is more intuitive:
  (KeyType,ValueType)[ ... ] 

  # Always try to test Type of the Dict, unless you say (Any, Any) :
  [ ... ] 

  # Delete the { ... } option 

What do you think?

@blakejohnson
Copy link
Contributor

I rather like the current syntax. You can still create Dict's via Dict{K,V}(...) if you prefer. The [...] vs {...} difference is not specific to Dict's, but to all arrays. MATLAB users are particularly comfortable with using curly brackets to denote a set of objects with non-uniform types/sizes.

@toivoh
Copy link
Contributor

toivoh commented Jan 17, 2013

I don't think that (KeyType=>ValueType)[...] should be changed to (KeyType,ValueType)[...], because it is more obvious with the former that you are creating a Dict (especially when creating an empty dict, (KeyType,ValueType)[] doesn't seem very obvious to me).

I agree that it's a bit unfortunate that the syntax (K=>V)[...] is so different from Dict{K,V}, but I'm not sure of how to change it in any way that wouldn't break something major. There was a pretty long discussion about the syntax in #1467, when these things were first introduced. Also, (K=>V)[...] is very much in line with typed vectors T[...]. Do you feel the same way about T[...] vs. Vector{T}?

@diegozea
Copy link
Contributor Author

I never used MATLAB, I didn't know the syntax [ ... ] { ... } comes from here, and more, I didn't know that syntax working on Julia's Array too. On my mind, I associated { ... } and types on Julia.
About the T[] notation against the Vector{T} notation, I have to confessed I have the next problem:

julia> a=Vector{Int}
Array{Int64,1}

julia> push!(a,23)
no method push!(CompositeKind,Int64)

julia> a=Int[]
0-element Int64 Array

julia> push!(a,23)
1-element Int64 Array:
 23

But, after try one time on the list, I get the answer I don't have this problem anymore. I didn't think on Dict (...=>...)[] as a notation like this one. Now have more sense too me.
I agree about (KeyType=>ValueType)[] being more obvious and related with T[].

Maybe a better error message in

julia> (Int=>Int){}
unsupported or misplaced expression =>

can help to the cause too.

Maybe there is not a better option for Dicts... But the actual syntax was a little hard for me at first moment. I'm affray It is going to be hard to future users too :/ And first moments comes back when someone take distance for the language a couple of months sometimes. But... Maybe what we need is both things T[] (=>)[] better documented, and not a change on syntax (?)

@timholy
Copy link
Member

timholy commented Jan 17, 2013

a=Vector{Int}

This defines a as a type, not an instance of a Vector:

julia> a = Vector{Int}
Array{Int64,1}

julia> typeof(a)
CompositeKind

julia> b = Array(Int, 0)
0-element Int64 Array

julia> typeof(b)
Array{Int64,1}

julia> a == typeof(b)
true

In Julia you can do operations on types. That's truly awesome (and it lies at the core of how it's implemented), but you have to make sure you keep the distinction firmly in mind.

@timholy
Copy link
Member

timholy commented Jan 17, 2013

I would agree, however, that users coming from other languages can struggle in the early days with how to declare things---speaking for myself, at least.

At one point I started contributing a bunch of exercises to go along with the manual. I think they were causing some sort of trouble and ended up getting deleted or commented out, but perhaps we should resurrect and expand upon them.

@StefanKarpinski
Copy link
Member

At one point I started contributing a bunch of exercises to go along with the manual. I think they were causing some sort of trouble and ended up getting deleted or commented out, but perhaps we should resurrect and expand upon them.

The links to them didn't work and I couldn't quite figure out how to make them work at the time, so rather than leaving a lot of dangling links in the manual, I commented out the links. Recently, when Viral went through the manual, cleaning things up, I believe he may have removed the files. We could resurrect them with some spiffing up.

@StefanKarpinski
Copy link
Member

I'll point out that they definitely won't help with the Dict syntax though since that was added long after the examples were written.

@timholy
Copy link
Member

timholy commented Jan 17, 2013

I didn't mean to imply that my exercises were all that great, simply that this kind of trouble is one that having exercises might really help with. There's lots of evidence that "testing" helps learning.

If others agree with the principle, certainly we can start from scratch or expand upon them mightily.

@GlenHertz
Copy link
Contributor

How about something like

a = {}::Dict{String, Integer}

It is also nice to associate the type with a

[pao: Use backticks for inline code snippets; triple backticks each on their own line for blocks]

@StefanKarpinski
Copy link
Member

I don't really think that Dict syntax needs any more changes. It's a little complicated, but that's what happens when you have a sophisticated type system. Just use {a=>b} like other dynamic languages and everything will be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants