[swift-evolution] Make non-void functions @warn_unused_result by default

Rudolf Adamkovič salutis at me.com
Thu Feb 25 05:03:34 CST 2016


By the way, the following:

func updateIfNeeded() -> @convenience Int

… can satisfy a protocol requirement like:

func updateIfNeeded()

I’m no expert but … I love this!

R+

> On 25 Feb 2016, at 11:54, Rudolf Adamkovič <salutis at me.com> wrote:
> 
> +1 for @convenience attached to the return type.
> 
> Very, very clean solution.
> 
> R+
> 
>> On 25 Feb 2016, at 09:13, Nicola Salmoria via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> I agree that the attribute should be attached to the return type instead that to the function.
>> 
>> I’d say that the cases where it’s ok to discard the result are when the result is not strictly part of the API, but provided because it could be useful to the caller. An attribute that would naturally describe this could be
>> 
>> @convenience
>> 
>> The first example that comes to mind is Dictionary.updateValue. The declaration could then become
>> 
>>     public mutating func updateValue(value: Value, forKey key: Key) -> @convenience Value?
>> 
>> Nicola
>> 
>> > > @optional_result (or ideally @optionalResult if camel-case for attributes is accepted) is clearer I think if we want a shortened two-word form.
>> > I don't like "optional" here because it falsely implies a connection with the Optional type.
>> > 
>> > I do think, however, that this needs to be something very short—one or two words, not three or four. One way to trim words would be to attach the attribute to the return type; the position would imply that the keyword related to the return type, so the word "result" would be unnecessary:
>> > 
>> > @ignoreUnusedResult mutating func removeLast() ->Element
>> > mutating func removeLast() ->@ignoreUnused Element
>> > 
>> > That might be a little strange, though—we don't currently have any attributes in that position.
>> > 
>> > Another way to make this shorter is to try to find an adjective which implies *why* we can ignore the result—that is, because it isn't important. The problem is that most such adjectives are either compound words:
>> > 
>> > @unimportantResult mutating func removeLast() ->Element
>> > mutating func removeLast() ->@unimportant Element
>> > 
>> > @discardableResult mutating func removeLast() ->Element
>> > mutating func removeLast() ->@discardable Element
>> > 
>> > Or they're uncommon words people may need to look up:
>> > 
>> > @banalResult mutating func removeLast() ->Element
>> > mutating func removeLast() ->@banal Element
>> > 
>> > Actually, I can think of one word in the latter category that many programmers *do* know:
>> > 
>> > @trivialResult mutating func removeLast() ->Element
>> > mutating func removeLast() ->@trivial Element
>> > 
>> > --
>> > Brent Royal-Gordon
>> > Architechies
>> > 
>> > 
>> > 
>> > 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto: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/20160225/8dd7b019/attachment.html>


More information about the swift-evolution mailing list