'subscript' function can't be throwable?

15 views
Skip to first unread message

Jens Alfke

unread,
Jun 11, 2015, 1:56:10 PM6/11/15
to swift-l...@googlegroups.com
The current Swift 2 compiler doesn’t allow a subscript operator to throw exceptions; at least, if I declare it like this:

    public subscript(name: String) throws -> Something? { … }

I get the familiar “Consecutive declarations on a line must be separated by ‘;’” error at the “throws” keyword.

Is this an intentional design limitation, or just a compiler bug?

In my case, the subscript operator is looking up a value from a database given its key, so there are various ways it could fail aside from there not being a value.

—Jens

Chris Lattner

unread,
Jun 12, 2015, 1:59:22 PM6/12/15
to Jens Alfke, swift-l...@googlegroups.com
We haven’t discussed allowing subscripts and computed properties to be throwable yet, but I don’t see any obvious reason to to support them.  That said, supporting this would introduce some complexity into the model: We’d have to decide whether the getter and setter could have different throws markers.  While being able to express that would be great, allowing that would actually be quite hard and surprising for our model. We’d have to support protocol requirements, overload, etc.

In any case, we need to talk about this, we’ll do so over the next few weeks.

-Chris

Reply all
Reply to author
Forward
0 new messages