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

Added % operator for table construction #11702

Closed
wants to merge 1 commit into from

Conversation

zevv
Copy link
Contributor

@zevv zevv commented Jul 10, 2019

% is essentially the same thing as toTable, but allows for a more concise notation:

var a = %{"key_1": "value_1", "key_2": "value_2"}

I don't expect this PR to go through as-is because the % operator might be confused with the Json constructor, although it does not actually clash.

@mrhdias
Copy link
Contributor

mrhdias commented Jul 10, 2019

This syntax is similar to that used in perl. The developers already know this syntax for arrays and hashs.

var a = @[1, 2, 3] # like @arrays in perl
var h = %{"key_1": "value_1", "key_2": "value_2"} # like %hashes in perl

@timotheecour
Copy link
Member

timotheecour commented Jul 11, 2019

I find toTable more readable / searchable and less likely to be confused with existing %, more generally I usually prefer explicit names to operators (except for commonly accepted operator names) ; a module can always define its own alias operator

I don't expect this PR to go through as-is because the % operator might be confused with the Json constructor, although it does not actually clash.

it does:

 Error: ambiguous call; both tables.%(pairs: openarray[tuple of (A, B)]) [declared in /Users/timothee/git_clone/nim/Nim_temp2/lib/pure/collections/tables.nim(326, 6)] and json.%(elements: openarray[T]) [declared in /Users/timothee/git_clone/nim/Nim_temp2/lib/pure/json.nim(333, 6)] match for: (array[0..1, tuple of (string, string)])
    var a = %{"key_1": "value_1", "key_2": "value_2"}
import tables
import json

proc main()=
  var a = %{"key_1": "value_1", "key_2": "value_2"}
  echo a

main()

@juancarlospaco
Copy link
Collaborator

Seems confusing with the JSON one. 🤔 Maybe another operator, maybe on the Sugar module.

@Araq
Copy link
Member

Araq commented Jul 15, 2019

Well, sorry, rejected. JSON already took the %. And in general we don't add new crazy operators anymore.

@Araq Araq closed this Jul 15, 2019
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

Successfully merging this pull request may close these issues.

None yet

5 participants