-
-
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
Anonymous generic function inside of a generic function #19045
Comments
struct Test{
something string
}
struct Dependency {}
// struct Factory {
// build fn()!
//}
struct Factory[F, E] {
build fn(F) !E // NOTE: function signatures need to be consistent.
}
fn inject[T](mut serv T)!{
$for field in T.fields {
if field.typ is string {
serv.$(field.name) = "Hello world!"
}
}
}
fn use_factory[F, E](factory fn(dep F) !E) Factory{
return Factory{
build: fn [factory][F]()!{
mut dep := F{}
inject[F](mut dep)!
dump(factory(dep)!)
}
}
}
use_factory[Dependency, Test](fn (dep Dependency)!Test {
return Test{
something: 'daleks!'
}
}).build()! |
Hi, first what you did is not what i wish, i don't know why you put the same function in the anonymous that the parameter.
The fix of the problem is pass the two generics to the anonymous function, but the problem/bug is the error of v, as is complettly missdirection
…On Thursday, 3 August 2023, shove wrote:
```v
struct Test{
something string
}
struct Dependency {}
// struct Factory {
// build fn()!
//}
struct Factory[F, E] {
build fn(F) !E // NOTE: function signatures need to be consistent.
}
fn inject[T](mut serv T)!{
$for field in T.fields {
if field.typ is string {
serv.$(field.name) = "Hello world!"
}
}
}
fn use_factory[F, E](factory fn(dep F) !E) Factory{
return Factory{
build: fn [factory][F]()!{
mut dep := F{}
inject[F](mut dep)!
dump(factory(dep)!)
}
}
}
use_factory[Dependency, Test](fn (dep Dependency)!Test {
return Test{
something: 'daleks!'
}
}).build()!
```
--
Reply to this email directly or view it on GitHub:
#19045 (comment)
You are receiving this because you authored the thread.
Message ID: ***@***.***
--
Sent from my Sailfish device
|
I tried to reproduce, but now it works fine Some test code: fn (mut db Database[T]) remove_by_id(id int) {
db.data = db.data.filter(fn [id][T](ele T) bool {
return ele.id != id
})
} |
With latest V, I see both a notice and an error (that doesn't make sense):
|
@KamkoAmoh the problem is when you have multiple generics. @JalonSolov yes there was this warning/error, but is missleading, as the problem is the caller that only has 1 generic parameter, when you put both works. (you can see this is the same as the original, with only adding second generic in the anonymous function: https://vosca.dev/p/074d669643 ) |
Describe the bug
Code: https://vosca.dev/p/fb9c42f729
When you have a generic function that accepts two types, and has an anonymous function inside, If you put only one type (and not the second, as this case that is used both), it give you a weird error.
I hope the people that knew better V-lang, can explain better the problem
Expected Behavior
Complain that the anonymous function misses the second generic parameter
Current Behavior
Output:
Reproduction Steps
For what I found you need a function, that creates an anonymous function that handles both types
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.0 8ee1667
Environment details (OS name and version, etc.)
The text was updated successfully, but these errors were encountered: