File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change @@ -3747,7 +3747,7 @@ fn main() {
3747
3747
3748
3748
``` v wip
3749
3749
3750
- struct Repo<T> {
3750
+ struct Repo[T] {
3751
3751
db DB
3752
3752
}
3753
3753
@@ -3763,14 +3763,14 @@ struct Post {
3763
3763
body string
3764
3764
}
3765
3765
3766
- fn new_repo<T> (db DB) Repo<T> {
3767
- return Repo<T> {db: db}
3766
+ fn new_repo[T] (db DB) Repo[T] {
3767
+ return Repo[T] {db: db}
3768
3768
}
3769
3769
3770
3770
// This is a generic function. V will generate it for every type it's used with.
3771
- fn (r Repo<T> ) find_by_id(id int) ?T {
3771
+ fn (r Repo[T] ) find_by_id(id int) ?T {
3772
3772
table_name := T.name // in this example getting the name of the type gives us the table name
3773
- return r.db.query_one<T> ('select * from ${table_name} where id = ?', id)
3773
+ return r.db.query_one[T] ('select * from ${table_name} where id = ?', id)
3774
3774
}
3775
3775
3776
3776
db := new_db()
@@ -3782,7 +3782,7 @@ post := posts_repo.find_by_id(1)? // find_by_id<Post>
3782
3782
3783
3783
Currently generic function definitions must declare their type parameters, but in
3784
3784
future V will infer generic type parameters from single-letter type names in
3785
- runtime parameter types. This is why ` find_by_id ` can omit ` <T> ` , because the
3785
+ runtime parameter types. This is why ` find_by_id ` can omit ` [T] ` , because the
3786
3786
receiver argument ` r ` uses a generic type ` T ` .
3787
3787
3788
3788
Another example:
You can’t perform that action at this time.
0 commit comments