-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Proposal: Ziglang Infix Function Sugar #8204
Comments
(discussed @ meetings 10 Mar 2021) |
Essentially a duplicate of #427. |
If a library exports a function like this, do you have to do |
lol sorry for the confusion, the decision was to close it, this will not be a part of zig. Consider it a "steel man" against the operators concept. |
The restriction to just one or two characters doesn't make much sense given the delimiteters and full names would often be nicer for certain things. Having |
This was a difficult decision, but ultimately we decided to close this issue. This proposal sidesteps the primary problem with operator overloading. With this proposal, it's clear to someone who knows Zig that a function is being called (not hidden control flow). It also has some nice properties. For programs which are highly mathematical in nature, the math formula is a form of documentation for the code. Having the code be closer to the formula may make it more readable, and could potentially prevent bugs. And it took us a solid 10 minutes to find the bug in the "current zig" example. However, it also took us quite a while to see the bug in the infix operators example. Finding the misplaced parenthesis is not trivial, even with operators. We think this is a better target for how this example would be written in status quo zig: sigma.mul(sqrt(2 * pi)).recip().mul(x.sub(mu).div(sigma).square().div(2).neg().exp()); In some ways, this feels clearer than the mathematical formula. The order of operations is primarily left to right, so there is little spiral reading needed. You don't need to mentally track many parentheses, it's clear what order things happen. It's true that this is very different from the mathematical representation of the formula, but it's certainly not unreadable. So we don't feel that there is a large amount to be gained from this change. Additionally, the problem of overload selection is difficult. #427 resolved this by doing a lookup into the namespace of the type on the left hand side. But that doesn't work with things like Ultimately, we think that the status quo solution is good enough. The |
Specific definition:
The expression
(a <+> b)
is parsed as equivalent to@"<+>"(a, b)
. The contents of the expression must be eitherone or two ascii characters taken from the range
A..Z
|a..z
|
0..9
| ['+', '-', '/', '*', '.']Motivation
There have been lots of people complaining that zig has no operator
overloading, and thus it is an unsuitable replacement for certain things
that C++ is good for (advanced math, some game physics stuff). I
suspect that people don't want operator overloading, they want a
binary infix form, so that they can easily spot logical errors and
debug them quickly. This proposal seeks to provide binary infix forms
while still satisfying the philosophy of zig and gently discouraging
undesired uses of operator overloading.
Spot the bug
Normal distribution function (presume you are using say fixed point
numbers so you can't use the standard operators):
(correct) math formula:
current zig (buggy):
with infix functions (buggy):
What this is not.
must be a
fn @"<+>"(a: ..., b: ...) ... {...}
defined "in thenormal way", in the proper scope.
(_ <+> _)
is to be understoodas "a function call".
memory allocation, the allocator must be defined by the parameters
and arbitrated by the function. Memory failures must then be either
caught within the function or directly bubbled up as an error in
the standard way that Zig handles errors.
Design details
enough to not be confused as a builtin operator.
concatenation".
zig knowing binary operator precedence. Also adds to the ugliness
to discourage overuse.
pull a
<+>
function from a struct, reassign it as<+a>
and thenexport a
<+>
that uses<+a>
.Details that don't have to be decided atm
Scope
Implementations
refactored the parser to be more DDD; it was about ~100 LOC including
tests; however was not able to test that it "actually works" due to stage
2 not working yet.
Pros
BFLOAT16 support #3148, Fixed-point number support? #1974, (maybe more) by means of "punt to a library".
is useful for cryptographic and reed-solomon erasure coding applications.
Cons
@"<+>"(a, b)
vs.(a <+> b)
The text was updated successfully, but these errors were encountered: