-
-
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
Broken operator overloading for aliased arrays #23213
Comments
I didn't know about this restriction but imho I like it and I think I understand it. Complex numbers are two dimensional numbers. You can't say if one complex number is greater than other but only if they are exactly equal or not. Arrays to me could represent multi dimensional numbers so also only |
I thought about this some more. I agree with jorgeluismireles. It seems that arrays really should support only Initially, I didn't even think about adding additional operators for regular arrays. But I would like to be able to do this for their aliases through operator overloading. Maybe I'm wrong and it won't work. If so, it seems strange that In any case, even if this issue is rejected, I can simply add |
The problem is in understanding what should happen if the operators are overloaded. For example Or does it mean every element in What if the array sizes are different? Is the shorter array "less than" the longer array? What if all the elements of the longer array are less than the smallest element of the other array? I could go on for a long time about all the possible items of confusion. |
Describe the bug
Only
!=
and==
operators allowed for arrays. But V allows operator overloading. I create an alias for a fixed array and overload<
.The documentation says that this should be enough to generate the
>
,<=
,>=
. It seems like that's how it should work.For now V treats such an alias as an array (which is correct) and forbids me from using
<=
,>=
,>
,<
directly. But overloading works if type alias is used in struct (as expected lol). See example below.My usecase: the array of 4 bytes is an IPv4 address, which can also be represented by
u32
. Therefore, it is logical for it to have the ability to compare even if the address is represented by an array.Reproduction Steps
Run: https://play.vlang.io/p/b0138dd025
Output:
Output if
assert a > b
is uncommented:Expected Behavior
I expect to
>
,<
,<=
and>=
works for myAddr
type, i.e. the following code should work:This behavior does not violate the restrictions described in https://docs.vlang.io/limited-operator-overloading.html#restriction
Current Behavior
Checker forbids anything except
==
and!=
.Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.8 939d243
Environment details (OS name and version, etc.)
V full version: V 0.4.8 5ec9bb5.939d243
OS: linux, Linux version 6.6.8-arch1-1 (linux@archlinux) (gcc (GCC) 13.2.1 20230801, GNU ld (GNU Binutils) 2.41.0) #1 SMP PREEMPT_DYNAMIC Thu, 21 Dec 2023 19:01:01 +0000
Processor: 16 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i5-1240P
getwd: /home/ge
vexe: /home/ge/.vlang/v
vexe mtime: 2024-12-19 18:01:34
vroot: OK, value: /home/ge/.vlang
VMODULES: OK, value: /home/ge/.vmodules
VTMP: OK, value: /tmp/v_1000
Git version: git version 2.47.1
Git vroot status: weekly.2024.51-22-g939d243b (1 commit(s) behind V master)
.git/config present: true
CC version: cc (GCC) 14.2.1 20240910
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
Huly®: V_0.6-21649
The text was updated successfully, but these errors were encountered: