[swift-evolution] Generic Subscripts
Rien
Rien at Balancingrock.nl
Sat Jan 14 12:17:59 CST 2017
> On 14 Jan 2017, at 18:45, Anton Zhilin via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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.
Yup. This is what I do:
var json = VJson()
var name: String?
json[“books”][1][“title”] &= “THHGTTG”
and the reverse
name &= json[“books”][1][“title"]
The overloaded operator ‘&=‘ takes care of the types. (‘=‘ cannot be overloaded)
>
> 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
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
Regards,
Rien
Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl
More information about the swift-evolution
mailing list