-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Dummy variable _ #9343
Comments
The use of underscore, |
I would like this a lot. Code gets messy when I don't care about an output variable but have to explicitly reassign The function |
People already use _ and ignore it. I'm not sure about the utility to enforce further constraints/guarantee of its use/non-use. If performance is critical, and we find ourselves using _ in that context, I wonder if the function generating the unused value is too bloated and should be re-factored. |
The code generator can probably be improved for cases where a variable is never used. |
For function arguments, you can already use e.g.
to explicitly ignore one. Perhaps it should be allowed also for assignment locations, e.g.
One thing I like about |
A combination of the improvement to code generation that @JeffBezanson suggests and knowing the trick that @toivoh points out (is it in the manual somewhere?) would probably resolve any of my issues. |
I looked into it and I think we already optimize out unused variables. The case described above probably produced the |
…9343) this builds and passes tests with LLVM assertions, but we should be on the lookout for regressions here.
+1 for @toivoh's suggested syntax. |
Closing as this is better handled by forms of static analysis, not syntax. |
@jakebolewski, is there another issue which tracks the static analysis improvements needed for this? Or would it be better to reopen and change the title (or open another issue)? |
Here's one situation where it could be nice to have an explicitly ignored identifier name: julia> foo() do _, _
println("ignores arguments")
end
ERROR: syntax: function argument names not unique |
I'm reopening this, as I've encountered more and more cases where it would be convenient for the front end to be able to emit a pseudo-name for a value that gets ignored. Example 1: The syntax Example 2: On the jb/functions branch all functions are generic functions, so I want to normalize the syntaxes There are a few ways in which the full version of this feature is nicer than relying on optimizations:
|
This also sounds like a better and more general solution to #12474 |
I'm on board. I guess this would conflict with potentially using |
That would be a bit confusing. It would be nice to be able to give "error: _ used in value position" errors. |
If we allow that, it effectively reserves value position usage of |
I was thinking the other day to port a PHP MIT licensed version of gettext to Julia, gettext use I don't know what do you think about this? I haven't mentioned anywhere because I haven't even started implemented it yet, but it's one step in my efforts to explore multilang Julia here. I'm just way too slow, wish I had more time. |
+1 for gettext integration, but |
@nalimilan yes I was talking about that PHP implementation, it's the only MIT licensed one I found which looked self contained. I'll experiment with Thanks for your advice! |
Actually
I wonder about it's performance. |
The non-standard string literall |
Thank you, I'll try to do that. |
it seems most of the goals of #9343 (comment) were implemented (via the hidden symbol '#unused#') in jb/functions (and improved upon in jb/linear3) |
Following on @vtjnash's comment, for |
If we're going to do this then perhaps using |
yes |
(remove "won't fix" label?) |
Then the first steps for 0.6 are:
This allows people to start using |
Do we really need to go through that kind of deprecation cycle for this? I suspect very few people are using |
Sure, we could skip the deprecation cycle and just make it an error right away. |
As mentioned in #20446, another possible use of |
…ble name; in later versions of Julia the plan is to use _ for this sort of thing
It'd be nice for |
It'd be convenient to have a designated dummy variable
_
(actually keyword), which would simply ignore everything stored into it. For example, you often want to deconstruct tuples like sowhere the
_
values are uninteresting. Likewise, one may want to ignore function arguments, like soThis is actually a syntax error right now, because two arguments have the same name, which makes sense if you actually want to read their values. The idea here is to instead discard them, and reading the value of
_
would be a syntax error.In theory, this is purely about code readability: when one sees
_
, one knows that the value is not used and thus won't need to be thought about any further; OTOH if one sees something else, then the value is probably going to be used further down the line and needs to be kept track of. One also won't need to behold the unsightly output of wetware gensym() implementations in unused function argument names. In practice though, there is also a performance difference with the current compiler:The assembly for f2 is significantly shorter.
The text was updated successfully, but these errors were encountered: