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

Different "?" syntax required when indexing maps with sumtypes and without #13256

Open
jdonnerstag opened this issue Jan 23, 2022 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@jdonnerstag
Copy link
Contributor

V version: V 0.2.4 359b674.66a67de
OS: windows, Microsoft Windows 10 Enterprise v19042 64-bit

What did you do?
I updates a module of mine vlang-yaml to test it against the latest V version

What did you expect to see?
Run my test cases like v -stats test ., and using -stats to also see all the compiler warnings.

What did you see instead?
The error message is clear: Append ? like so x1.obj["national"]?. See below

What is wrong: Only when indexing a map with a sum type, you need to handle the option (error). If not a sum type, it is not necessary. That is inconsistent. Either the errors must be handle when indexing a map or not. My preference:

x := mymap["a"]
x := mymap["a"] or { mydefault }
x := mymap["a"] or { return error("..") }
step3_reader_test.v:58:15: warning: `or {}` block required when indexing a map with sum type value
   56 |             assert x2.ar[2] == YamlValue("New York Yankees")
   57 |         }
   58 |         x3 := x1.obj["national"]
      |                     ~~~~~~~~~~~~
   59 |         assert x3 is YamlListValue
   60 |         if x3 is YamlListValue {
@jdonnerstag jdonnerstag added the Bug This tag is applied to issues which reports bugs. label Jan 23, 2022
@dumblob
Copy link
Contributor

dumblob commented Mar 3, 2022

Yep, this is one of the few exceptions. Allegedly this exception is there to make use of maps easier at the cost of pushing compile-time safety to run-time panic (i.e. also a kind of "safety").

But I don't like this strawman reasoning as we can easily propagate the error with ? (or in the future with ! which though has a different meaning as of now) as in x := mymap["a"]?["b"]?["c"]? which if required by compiler is just one character more than x := mymap["a"]["b"]["c"] and makes it explicit and safe before even executing the resulting app.

We shouldn't also forget about the case if it's an l-value. But that's an old discussion and V lang differs now in a way we could easily use ? or ! as noted above.

Feel free to push harder for this than I'm able to 😉!

Another slightly related topic: #10332 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

2 participants