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

Can't use an expression as a dict key? #5231

Closed
Akaricchi opened this issue Apr 9, 2019 · 7 comments
Closed

Can't use an expression as a dict key? #5231

Akaricchi opened this issue Apr 9, 2019 · 7 comments

Comments

@Akaricchi
Copy link
Contributor

wtf = 'wtf'
wtf = { wtf : wtf }

I would expect this code to be equivalent to:

wtf = { 'wtf' : 'wtf' }

Instead, it raises a parsing error:

meson.build:67:8: ERROR: Key must be a string.
wtf = { wtf : wtf }

Seems like an unreasonable limitation?

@nirbheek
Copy link
Member

nirbheek commented Apr 9, 2019

This is a known limitation and it's documented too: Keys are required to be literal strings, values can be objects of any type.

IIRC this was done because it was easier to implement at the time. I'm sure we'd be happy to accept patches to enable any object to be inserted as a key since almost all objects are immutable in Meson anyway.

@xclaesse
Copy link
Member

xclaesse commented Dec 3, 2019

Already fixed by this PR,I think: #6166

marc-h38 pushed a commit to marc-h38/meson that referenced this issue Dec 4, 2019
Fixes PR mesonbuild#6166 and more specifically commit 4e460f0 that tried to
make sure the type of a key variable is a string but checked the type of
the value instead. Extends test common/228's limited coverage,
its only test case had (surprise) a string value. Also avoid reserved
python keyword 'dict' and potentially confusing string 'key'.

Implements mesonbuild#5231 for real.
@marc-h38
Copy link
Contributor

marc-h38 commented Dec 4, 2019

Already fixed by this PR,I think: #6166

Almost, see PR #6279 above (tagging @scivision)

In the mean time and for people with meson_version <= 0.52, re-sharing a smart workaround @eli-schwartz shared on IRC:

str_keys = [['one', '1'], ['two', '2'], ['three', '3']]
foreach key in str_keys: do_things(key[0], key[1])

This is basically re-implementing a dict manually with a multidimensional list. A bit tedious, but nowhere near as tedious as "unrolling" the foreach loop and Duplicating and Diverging a large do_things.

@eli-schwartz
Copy link
Member

Oh, cool, I can drop my multidimensional lists soon. \o/

xclaesse pushed a commit that referenced this issue Dec 4, 2019
Fixes PR #6166 and more specifically commit 4e460f0 that tried to
make sure the type of a key variable is a string but checked the type of
the value instead. Extends test common/228's limited coverage,
its only test case had (surprise) a string value. Also avoid reserved
python keyword 'dict' and potentially confusing string 'key'.

Implements #5231 for real.
@xclaesse
Copy link
Member

xclaesse commented Dec 4, 2019

oh wait, actually += operator got fixed, not normal assignation.

k = 'somekey'
d = {k:1}
message(d[k])

-> ERROR: Key somekey is not in dict

@xclaesse
Copy link
Member

xclaesse commented Dec 4, 2019

Ok, I did a real refactor of dict: #6281

There was too much code duplication for my taste, and I found a few inconsistencies like:

# This was not raising error
k = 'a'
d = {k:1, 'a':2}
# This was not allowed, as well as any function call that returns string
d = {'a' / 'b':1}

@scivision
Copy link
Member

I believe #6281 fixed this issue

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

No branches or pull requests

6 participants