mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
docs: change <> to [] in docs.md (#16632)
This commit is contained in:
parent
7c02274754
commit
ad5290677c
12
doc/docs.md
12
doc/docs.md
@ -3747,7 +3747,7 @@ fn main() {
|
|||||||
|
|
||||||
```v wip
|
```v wip
|
||||||
|
|
||||||
struct Repo<T> {
|
struct Repo[T] {
|
||||||
db DB
|
db DB
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3763,14 +3763,14 @@ struct Post {
|
|||||||
body string
|
body string
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_repo<T>(db DB) Repo<T> {
|
fn new_repo[T](db DB) Repo[T] {
|
||||||
return Repo<T>{db: db}
|
return Repo[T]{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a generic function. V will generate it for every type it's used with.
|
// This is a generic function. V will generate it for every type it's used with.
|
||||||
fn (r Repo<T>) 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
|
table_name := T.name // in this example getting the name of the type gives us the table name
|
||||||
return r.db.query_one<T>('select * from ${table_name} where id = ?', id)
|
return r.db.query_one[T]('select * from ${table_name} where id = ?', id)
|
||||||
}
|
}
|
||||||
|
|
||||||
db := new_db()
|
db := new_db()
|
||||||
@ -3782,7 +3782,7 @@ post := posts_repo.find_by_id(1)? // find_by_id<Post>
|
|||||||
|
|
||||||
Currently generic function definitions must declare their type parameters, but in
|
Currently generic function definitions must declare their type parameters, but in
|
||||||
future V will infer generic type parameters from single-letter type names 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 `<T>`, because the
|
runtime parameter types. This is why `find_by_id` can omit `[T]`, because the
|
||||||
receiver argument `r` uses a generic type `T`.
|
receiver argument `r` uses a generic type `T`.
|
||||||
|
|
||||||
Another example:
|
Another example:
|
||||||
|
Loading…
Reference in New Issue
Block a user