Skip to content

Commit 821d30b

Browse files
authored
Improve find_by_id comments
I now think explaining `db.query_one` with a comment is better than including a definition without implementation.
1 parent 8ea720c commit 821d30b

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

website/templates/docs.html

+13-8
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,9 @@ <h2 id=option>Option types</h2>
565565

566566

567567
<h2 id=generics>Generics</h2>
568-
<pre>struct Repo &lt;T> {
568+
<pre>import database
569+
570+
struct Repo &lt;T> {
569571
db DB
570572
}
571573

@@ -576,25 +578,28 @@ <h2 id=generics>Generics</h2>
576578
return Repo&lt;T>{db: db}
577579
}
578580

579-
fn (db DB) query_one&lt;T>(query string, id int) T? {
580-
<comment>// Return a value of type T from the database, or an error.
581-
// Implementation omitted...</comment>
582-
}
583-
584-
<comment>// This is a generic receiver function. V will generate it for every Repo instance it's used with.
585-
// T is inferred from the receiver argument. Again &lt;T> is implied by just writing Repo:
581+
<comment>// This is a generic method. V will generate it for every Repo instance it's used with.
582+
// Again &lt;T> is implied by just writing Repo for the receiver type, so V makes the method take &lt;T> too:
586583
// fn (r Repo&lt;T>) find_by_id&lt;T>(id int) T?</comment>
587584
fn (r Repo) find_by_id(id int) T? {
588585
table_name := T.name <comment>// in this example getting the name of the type gives us the table name</comment>
586+
<comment>//lookup a value of type T from the database, or return an error</comment>
589587
return db.query_one&lt;T>('select * from $table_name where id = ?', id)
590588
}
591589

590+
struct User { name string }
591+
struct Post { text string }
592+
592593
fn main() {
593594
db := new_db()
594595
users_repo := new_repo&lt;User>(db)
595596
posts_repo := new_repo&lt;Post>(db)
597+
<comment>// find_by_id<User> is inferred from users_repo:</comment>
596598
user := users_repo.find_by_id(1)?
599+
<comment>// find_by_id<Post> is inferred from posts_repo:</comment>
597600
post := posts_repo.find_by_id(1)?
601+
println(user.name)
602+
println(post.text)
598603
}
599604
</pre>
600605

0 commit comments

Comments
 (0)