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

Jens Persson jens at bitcycle.com
Wed Jul 5 20:18:43 CDT 2017


On Wed, Jul 5, 2017 at 6:52 PM, Zhao Xin <owenzx at gmail.com> wrote:

> 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.
>

This is not true, for example the following works:
extension BinaryInteger {
    var signumAsComputedProperty: Self {
        if self < 0 { return -1 }
        else if self > 0 { return 1 }
        else { return 0 }
    }
}

BinaryInteger is a prosocol. But the following will also work:
struct S {
    var computedPropertyReturningSelf: S { return self }
}

So I'm not sure what you mean.
/Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170706/3061c871/attachment.html>


More information about the swift-users mailing list