-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Safe unpack #2617
Comments
I like the general idea, but I have doubts about the syntax too. Since Following #132, one mitigation could be allowing pieces = line.split
(version, code), message = pieces, pieces[2]? Another alternative I see is having a version, code, message = line.split.values_at?(0, 1, 2) |
Being able to take any source, without need for special calls there (you might get, say, an array in an arg), and define optionality on the receiving destructured variables looks cleaner to me, whether |
@jhass True. On the other hand, using
Not that we have to do the same as in Ruby, but I think disallowing question marks and bangs in variable names is sane. With that, there would be no problem in using this syntax. It looks weird, maybe because it's not present in Ruby, but the use of Then of course this would also be possible: # This is already possible
obj.foo, obj.bar = exp
# This would be possible with this feature
obj.foo, obj.bar? = exp Again, |
|
The point is that |
I feel like |
Found it! #360 |
What if the syntax looks like: a, b, c, ? = whatever # note the extra comma |
@kirbyfan64 I think that the version, code, message ?= line.split
# or
version, code, message =? line.split
# or similar is rewritten to
_tmp = line.split
version = _tmp[0]?
code = _tmp[1]?
message = _tmp[2]? Though I don't think I like that much more. |
I like the idea. What if the
identifiers can't begin with Another alternative is that, the
So
or
might also be a solution the ambiguity if the trailing |
Hey, that "from now on idea"! Might play well for all of us? :-) some_mapish_thing = {some_key: "Un", unused_key: "Dos"}
# here `var: key` could be defined opposite in syntax ofc
(my_var: :some_key, ?, var2: :other_key) = some_mapish_thing |
You call this "safe" unpacking but I think having too many items and silently ignoring them is much more dangerous than a runtime error when there are too few items. And the code for catching this is not so nice, compared to just having to write Ideally, I would like the default to be a runtime ( compiletime for tuples?) error when the number of items doesn't exactly match, and then whatever other syntax options for leniency you might like. |
a=[]
a[99] #=> nil
a={}
a[99] #=> nil
nil.to_s #=> ""
nil + "9" #=> "9"
nil + 9 #=> 9
a[9]="ok"
a["9"] #=> "ok"
9 + "9" #=> "99"
"9" * "9" #=> 81 #because string has no * operator I like a language like this , and syntax like ruby , and performance like cpp . how about this ? |
"Pick one" @sevk - The comment is completely unrelated to the issue, and the proposals are Javascript-Batman'ish (NaNNaNNaNNa...). |
cough Back on topic... cough Would something like Python's syntax work? a, b, *rest = (1, 2, 3, 4)
# a: 1
# b: 2
# rest: (3, 4) Then you could ignore the extra by, say, assigning it to |
@kirbyfan64, that's how it should be for sure, but unfortunately the conversation isn't even going that direction. Currently |
I do think that adding I'd support either |
|
@asterite Ermm... why?! |
@Sija Makes the language more complex, has very few use cases, can already be done with |
Right now this:
x, y, z = exp
is translated to this:
This is OK, but sometimes we aren't sure if
exp
will have values for all indices. For example, an HTTP response status line may consist of two or three pieces separated by newline. Right now we have to do something like this:It would be nice to have a way to fetch elements at a given index with
[]?
instead of[]
, to get anil
value instead of an exception.An idea I had for some time is to use
?
on the left-hand side:version, code, message? = line.split
The above would be rewritten to:
We could of course also use it in block arguments:
I believe all of this can be useful and makes code more concise and clear, with less checks. I'm still not sure about the syntax, though, it might look confusing and it's yet another thing to know in the language. But a few times I felt I needed this in some code.
Thoughts?
The text was updated successfully, but these errors were encountered: