[swift-evolution] Generic Subscripts

Anton Zhilin antonyzhilin at gmail.com
Sat Jan 14 11:45:38 CST 2017

I’m not sure, but I think that in this case the specific type of these
values is determined at runtime.
Then a safe approach would be separate string: String?, bool: Bool?, int:
Int? computed properties, as it’s done in JSON parsers.

if let bookCount = row.value(named: "bookCount").int {
if let bookCount = row["bookCount"].int {
let bookCount = row.int("bookCount")!   // crash if database is corrupt

Additionally, this is an overall bad example of generics. Fields of
database tables can only map to a limited set of static types in Swift,
which are supported by database adapter.

2017-01-14 16:50 GMT+03:00 Gwendal Roué via swift-evolution <
swift-evolution at swift.org>:

This is a consequence of your vision of subscript. If interesting, it is
> also limiting for no real purpose.
> As the developer of a Swift database library, I'd like to offer a better
> API than the following:
>     // Current state of affairs
>     let name: String = row.value(named: "name")
>     let bookCount: Int = row.value(named: "bookCount")
>     let hasBooks: Bool = row.value(named: "bookCount")
> Instead, I wish I could offer GRDB.swift would let its users write:
>     // With improved subscripts
>     let name: String = row["name"]
>     let bookCount: Int = row["bookCount"]
>     let hasBooks: Bool = row["bookCount"]
> And this requires genericity on return type.
> Gwendal
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170114/2dcaf139/attachment.html>

More information about the swift-evolution mailing list