[swift-evolution] Syntax extensions for indicating mutation (was: [Review] SE-0023 API Design Guidelines)
Dave Abrahams
dabrahams at apple.com
Fri Jan 29 14:23:45 CST 2016
on Fri Jan 29 2016, Adriano Ferreira <adriano.ferreira-AT-me.com> wrote:
> Indeed, Ruby has an interesting convention where a question mark (?) is used for boolean functions/methods:
>
> In Swift: foo.contains(...)
> In Ruby: foo.include?(...)
>
> In Swift: foo.isEmpty
> In Ruby: foo.empty?
>
> Well, in the last case `isEmpty` is a property whereas `empty?` is a method, but the idea is similar.
>
> Also, an exclamation mark (!) is generally used to indicate that a function/method mutates `self`:
>
> Non-mutating:
>
> Swift — foo.reverse()
> Ruby — foo.reverse
>
> Mutating:
>
> Swift — foo.reverseInPlace()
> Ruby — foo.reverse!
>
> Non-mutating:
>
> Swift — foo.sort() or foo.sort({…})
> Ruby — foo.sort or foo.sort {…}
>
> Mutating:
>
> Swift — foo.sortInPlace() or foo.sortInPlace({…})
> Ruby — foo.sort! or foo.sort! {…}
>
> I think it’s a simple and nice way of addressing the naming issue of
> mutating vs. non-mutating or pure vs. impure
> functions/methods. However, this conflicts with the syntax sugar of
> Optionals and, therefore, following this path would have a clear
> impact in the language.
Please keep discussion of new language features in a separate thread;
thanks.
-D
--
-Dave
More information about the swift-evolution
mailing list