-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
V now has limited operator overloading #204
Comments
Great! What about % modulus and unary
This might be too restrictive, it disallows some reasonable cases that call a function for code reuse rather than algorithmic complexity.
Good idea. Also, for #174 we might want to overload other operators like shift and bitwise operations. For #203 we might want to overload |
I assume e.g. More potential operators:
Edit: Vec doesn't need == overloading. |
Great news! However, I consider the syntax you've proposed to be somewhat inconsistent with the language philosophy. Namely, I think operator tokens shouldn't be used as the names of functions which define the overloads. There are two main reasons why this is not a good practice:
I think the Rust approach to operator overloading is so far the sanest one I've seen, so please consider following that approach in V. (See also what the Rust by example book has to say on the subject). |
Rust's solution is interesting, thanks. Trait implementations generally are very flexible, but V doesn't have them (ATM). In #205 I propose implementing all binary operator overloading with a single template. It also supports the |
@medvednikov
I hope it's temporary. 😄 |
Operator [] (array index) may be even more useful than basic arithmetic. |
The syntax had to be either So I went for an easier option. I want to be very careful with this feature. That's why for now there will be no unary operators (use Please remember that V's goal is to be a simple and readable language that can be learned in half an hour. Sometimes it means more typing. There are lots of complex languages already. |
This code successfully compiled:
Please don't restrict calling other functions, it's can be very useful. |
Operator overloading goes against V’s philosophy of simplicity and predictability. But since scientific and graphical applications are among V’s domains, operator overloading is very important to have in order to improve readability:
a.add(b).add(c.mul(d))
is a lot less readable thana + b + c * d
.To improve safety and maintainability, operator overloading has several limitations:
https://vlang.io/docs#op
The text was updated successfully, but these errors were encountered: