[icfp] note: typed/racket db imports
This commit is contained in:
parent
59b0df38d9
commit
8250705ecb
|
@ -15,6 +15,46 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@section{Database}
|
||||||
|
|
||||||
|
Note: Typed Racket users can already specialize functions from the @tt{db} library
|
||||||
|
by giving them exact types in an import statement:
|
||||||
|
|
||||||
|
@racketblock[
|
||||||
|
(require/typed db
|
||||||
|
(#:opaque Connection connection?)
|
||||||
|
(postgresql-connect (->* () (#:user String #:database String) Connection))
|
||||||
|
(query-row (-> Connection String Any * (Vector Natural String)))
|
||||||
|
(query-maybe-row (-> Connection String Any * (Option (Vector Natural String))))
|
||||||
|
....)
|
||||||
|
]
|
||||||
|
|
||||||
|
The above statement asserts that @racket[query-row] only returns rows containing
|
||||||
|
a natural number followed by a string.
|
||||||
|
To work with multiple tables in the same module, a programmer can re-import
|
||||||
|
@racket[db] functions using a different name and type.
|
||||||
|
|
||||||
|
@todo{cohernet table names}
|
||||||
|
@racketblock[
|
||||||
|
(require/typed (prefix-in w: db)
|
||||||
|
(w:query-row (-> Connection String Any * (Vector Natural String))))
|
||||||
|
|
||||||
|
(require/typed (prefix-in s: db)
|
||||||
|
(s:query-row (-> Connection String Any * (Vector Natural Natural))))
|
||||||
|
]
|
||||||
|
|
||||||
|
@todo{Other languages? also follow this approach}
|
||||||
|
|
||||||
|
The benefits of our approach are:
|
||||||
|
@itemlist[
|
||||||
|
@item{
|
||||||
|
Single point of control for database types, instead of spreading types
|
||||||
|
across identifiers in the @racket[require/typed] statement.
|
||||||
|
}
|
||||||
|
@item{
|
||||||
|
Compile-time validation of query strings.
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user