-
Notifications
You must be signed in to change notification settings - Fork 29
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
More Operator Overloading #297
Comments
We thought about that some time ago. Someone else looking at this will have no idea what is actually happing since there is no jumptodecl for [] and (). Granted, this is the same for the arithmetic operators, but I havn't seen it misused yet and it makes using vector much more easy to read and write. However writing:
instead of
Doesn't seem as an improvement to me.. Also, Lists are mostly for looping. If you want direct access to values other datatypes are probably faster (like arrays, duh). |
Well, the list was more an abstract example. It would be good to have those operators in general, because it really depends on the data structure if those are feasable. Example:
Or consider a Matrix class where you want to have 2d indexing:
So actually an operator[] would improve readability a lot more than the +-*/ operators do already. The jumptodecl has the same problem with those aritmetic operators, so thats not really an argument. Nes' coding is so unreadable because of his excessive macro/module abuse and abuses like faking this pointers and so on. Plus sacrificing everything for speed. I don't think those operators would lead programmers into this direction. It is always the responsibility of the programmer to make his functions do the "intuitive correct" thing. You can also abuse the arithmetic operators to do completly unintuitive things. Or any other function which has name "doA" but does B instead. Such problems simply can't be avoided by any language, and therefore shouldn't IMHO. There will be always bad styled code, but this shouldn't limit the capabilities of a language IMO. |
Overloading |
Ok, I guess you are right, operator[] should be enough. It would just be cool if you could make it so that it can take an arbitrary number of arguments. So that you can for example write for a matrix class something like:
Because this way the need of a proxy class to allow 2d-indexing could be avoided. |
I'm not a fan of overloading function declaration, but I do like []. Can we have overloading for <, >, <=, >= operators? There is a fairly well-defined convention for comparing tuples, for example:
|
I think for the comparison operator it would be nicer to link the operators to a comparable interface or type-class. It would feel strange to overload <, >, <=, >= and == with 5 separate functions. Defining < for vec2 does not make sense in my opinion, because there is no default linear order. Some people might expect that the comparison is done by length instead of lexicographically. |
Yes, I would be happy with that as well. Seems like more work for you though :P
vec2 always has length of 2. For a template type like pair<T, T>, I agree with you, but for vectors of numbers I think the convention I mentioned above is really quite standard. Python:
I did think of one exception - partial ordering https://en.wikipedia.org/wiki/Vector_clock#Partial_ordering_property I think it makes sense for vec2 to have at least some default ordering. |
A Python tuple is something more generic. You can use tuples for many different things, so there will be some cases where lexicographical ordering makes sense. But vec2 is really supposed to be a vector with 2 dimensions and there is no standard ordering for this domain. I also cannot think of an example where you want to ask a question like: "Is the position of unit A less than the position of unit B?". Comparing positions like this just does not make sense. |
Okay, I think that's fair. I argue that vec2 isn't necessarily for representing location of unit, but I can see that for the typical use case, having an ordering could do more harm than good. |
Hi there,
I would like to suggest new language features, namely more operator overloading. It would be nice to be able to overload [] and () operators to create array-like access to data structures and functors. Examples:
And keep the good work :)
The text was updated successfully, but these errors were encountered: