Design guidelines for computed property vs no-arg method


(Jens Persson) #1

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


(Dave Abrahams) #2

As a name, signum falls into the “term-of-art” category, so at least
that part is in conformance to the guidelines. In specialized areas
like this one, I generally defer to the domain experts, and IIRC our
numerics people thought a function was more appropriate. In general,
though, we were unable to come up with solid guidelines for the use of
properties vs. functions. You can read more about this in the thread
that starts here:
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/007927.html

Cheers,

···

on Wed Jul 05 2017, Jens Persson <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?

--
-Dave


(Zhao Xin) #3

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jens Persson) #4

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

···

On Wed, Jul 5, 2017 at 6:52 PM, Zhao Xin <owenzx@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.


(Jens Persson) #5

Thanks, I'll read that thread.
I am however noting that FloatingPoint.sign is a computed property.
Seems to me like the "term-of-art" category is a rather fussy and
complicated/complicating concept.
/Jens

···

On Wed, Jul 5, 2017 at 11:50 PM, Dave Abrahams via swift-users < swift-users@swift.org> wrote:


As a name, signum falls into the “term-of-art” category, so at least
that part is in conformance to the guidelines. In specialized areas
like this one, I generally defer to the domain experts, and IIRC our
numerics people thought a function was more appropriate. In general,
though, we were unable to come up with solid guidelines for the use of
properties vs. functions. You can read more about this in the thread
that starts here:
https://lists.swift.org/pipermail/swift-evolution/
Week-of-Mon-20160125/007927.html


(Dave Abrahams) #6

I understand. Unfortunately it is necessary unless we are going—for
example—to start spelling out sine(x) and cosine(x), which would not be
acceptable to important constituencies of the language.

···

on Wed Jul 05 2017, Jens Persson <swift-users-AT-swift.org> wrote:

On Wed, Jul 5, 2017 at 11:50 PM, Dave Abrahams via swift-users < > swift-users@swift.org> wrote:


As a name, signum falls into the “term-of-art” category, so at least
that part is in conformance to the guidelines. In specialized areas
like this one, I generally defer to the domain experts, and IIRC our
numerics people thought a function was more appropriate. In general,
though, we were unable to come up with solid guidelines for the use of
properties vs. functions. You can read more about this in the thread
that starts here:
https://lists.swift.org/pipermail/swift-evolution/
Week-of-Mon-20160125/007927.html

Thanks, I'll read that thread.
I am however noting that FloatingPoint.sign is a computed property.
Seems to me like the "term-of-art" category is a rather fussy and
complicated/complicating concept.

--
-Dave