[swift-users] Design guidelines for computed property vs no-arg method

Zhao Xin owenzx at gmail.com
Wed Jul 5 11:52:55 CDT 2017


For me, a noun is a property, a verb is a method. So `foo` is a property,
`calculateFoo()` is a method.

For your specific question, `func signum() -> Self` returns `Self`, which
can't be used in property.
`var signum:Self { return self }` will generate an error "'Self' is only
available in a protocol or as the result of a method in a class".
So computed property can't be used here.

Zhao Xin

On Wed, Jul 5, 2017 at 7:57 PM, Jens Persson via swift-users <
swift-users at swift.org> wrote:

> Why is eg the BinaryInteger.signum() a method and not a computed property?
>
> public protocol BinaryInteger … {
>     /// Returns `-1` if this value is negative and `1` if it's positive;
>     /// otherwise, `0`.
>     ///
>     /// - Returns: The sign of this number, expressed as an integer of the
> same
>     ///   type.
>     public func signum() -> Self
> }
>
> The Swift API Design Guidelines doesn't say very much about computed
> property vs method with no arguments, but it seems like signum() violates
> them, no?
>
> /Jens
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170706/4528f934/attachment.html>


More information about the swift-users mailing list