File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3747,7 +3747,7 @@ fn main() {
37473747
37483748``` v wip
37493749
3750- struct Repo<T> {
3750+ struct Repo[T] {
37513751 db DB
37523752}
37533753
@@ -3763,14 +3763,14 @@ struct Post {
37633763 body string
37643764}
37653765
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}
37683768}
37693769
37703770// 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 {
37723772 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)
37743774}
37753775
37763776db := new_db()
@@ -3782,7 +3782,7 @@ post := posts_repo.find_by_id(1)? // find_by_id<Post>
37823782
37833783Currently generic function definitions must declare their type parameters, but in
37843784future 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
37863786receiver argument ` r ` uses a generic type ` T ` .
37873787
37883788Another example:
You can’t perform that action at this time.
0 commit comments