diff --git a/doc/docs.md b/doc/docs.md index 434e29b161..ca0933224a 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -3747,7 +3747,7 @@ fn main() { ```v wip -struct Repo { +struct Repo[T] { db DB } @@ -3763,14 +3763,14 @@ struct Post { body string } -fn new_repo(db DB) Repo { - return Repo{db: db} +fn new_repo[T](db DB) Repo[T] { + return Repo[T]{db: db} } // This is a generic function. V will generate it for every type it's used with. -fn (r Repo) find_by_id(id int) ?T { +fn (r Repo[T]) find_by_id(id int) ?T { table_name := T.name // in this example getting the name of the type gives us the table name - return r.db.query_one('select * from ${table_name} where id = ?', id) + return r.db.query_one[T]('select * from ${table_name} where id = ?', id) } db := new_db() @@ -3782,7 +3782,7 @@ post := posts_repo.find_by_id(1)? // find_by_id Currently generic function definitions must declare their type parameters, but in future V will infer generic type parameters from single-letter type names in -runtime parameter types. This is why `find_by_id` can omit ``, because the +runtime parameter types. This is why `find_by_id` can omit `[T]`, because the receiver argument `r` uses a generic type `T`. Another example: