-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Sorting an array of references #21662
Comments
Use |
Just remove the mut - doh - the compiler error message is very confusing |
Unfortunately removing the mut in my context does not work, consider this extended example (a distillation of my real case):
My real context is a lot more complex, but the above illustrates the problem. There is a conflict with 'mut' and the result is un-compilable, getting an error of:
How do I get over this? |
I added a comment to my bug report. Do you get to see those given the bug
report has been closed?
My comment suggests there is still a problem.
I apologize if I've broken some etiquette, but I'm a bit naive when
comes to using forums.
…On Fri, 14 Jun 2024 at 09:58, Delyan Angelov ***@***.***> wrote:
Use t.items.sort_with_compare(fn (a &&Thing, b &&Thing) int {return 0})
—
Reply to this email directly, view it on GitHub
<#21662 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEEESADMMPYHIWII66VZ7YDZHKWDZAVCNFSM6AAAAABJC3SBJKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNRXGU3TKNRSGI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Yes, notifications are still sent (and seen) even if the issue has been closed. You did nothing "wrong". Perhaps it would be more "correct" to re-open the issue if you're still having problems. |
struct Thing {
mut:
a int = 2
b int = 4
av int
}
fn (mut t Thing) average() int {
t.av = (t.a + t.b) / 2
return t.av
}
struct Things {
mut:
items []&Thing
}
fn (mut t Things) sort() {
t.items.sort_with_compare(fn (a &&Thing, b &&Thing) int {
if (*a).average() > (*b).average() {
return 1
} else if (*a).average() < (*b).average() {
return -1
}
return 0
})
}
fn main() {
mut t := Things{}
t.items << &Thing{1, 2, 0}
t.items << &Thing{5, 7, 0}
t.items << &Thing{2, 4, 0}
t.sort()
println(t)
}
PS D:\Test\v\tt1> v run .
Things{
items: [&Thing{
a: 1
b: 2
av: 1
}, &Thing{
a: 2
b: 4
av: 3
}, &Thing{
a: 5
b: 7
av: 6
}]
} |
Thanks for the workaround. De-referencing works, but why?
But by my niaive understanding of V I'd expect this to work:
Or at least provide a more helpful compiler error. |
btw, sorting with a comparison function, that modifies the compared items, is imho asking for trouble in general, since you do not know in advance on which items it will be called, how many times, etc. |
The real 'average' function that highlighted this issue calculates something that is expensive, so its evaluation is 'lazy', done on demand on the first call, once calculated it never changes. The sort algorithm calls it multiple times. The real application may, or may not, call it depending on context. I think that is a legitimate use case. But thanks for the speedy fix. I continue to be impressed... |
Describe the bug
Source does not compile
Reproduction Steps
Attempt to compile this:
Expected Behavior
It should compile
Current Behavior
Get error: sort_with_compare callback function parameter
a
with type&Thing
should be&&Thing
Possible Solution
I could not find a workaround
Additional Information/Context
No response
V version
V 0.4.6 100b3b0
Environment details (OS name and version, etc.)
V full version: V 0.4.6 294f7e4.100b3b0
OS: linux, "EndeavourOS Linux"
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 5800H with Radeon Graphics
getwd: /home/dave
vexe: /home/dave/v/v
vexe mtime: 2024-06-10 18:48:53
vroot: OK, value: /home/dave/v
VMODULES: OK, value: /home/dave/.vmodules
VTMP: OK, value: /tmp/v_1000
Git version: git version 2.45.1
Git vroot status: weekly.2024.14-438-g100b3b0f
.git/config present: true
CC version: cc (GCC) 14.1.1 20240507
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5
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.
The text was updated successfully, but these errors were encountered: