ExpressibleByZeroLiteral


(Sasha Lopoukhine) #1

Hello everyone!

I'd like to discuss the constraint on Strideable. I see no reason to restrict Strideable types to non-circular types. It would be pretty neat/useful to be able to define a type such as a Week, with strides looping around to the start of the enum. Currently, the only reason this is impossible is because of the restriction on Stride that it must conform to SignedNumber.

I think that it might be worth looking into defining a new protocol specifically for the constraint on Strideable.Stride, something that looks like this:

/// Instances of conforming types can be subtracted, arithmetically
/// negated, and initialized from `0`.
///
/// Axioms:
///
/// - `x - 0 == x`
/// - `-x == 0 - x`
/// - `-(-x) == x`
public protocol StrideProtocol : ExpressibleByIntegerLiteral {

    /// Returns the result of negating `x`.
    prefix public static func -(x: Self) -> Self

    /// Returns the difference between `lhs` and `rhs`.
    public static func -(lhs: Self, rhs: Self) -> Self
}

public typealias SignedNumber = StrideProtocol & Comparable

Strideable.Stride could then conform to StrideProtocol, allowing groups etc.

WDYT?

-- Sasha


(Xiaodi Wu) #2

Have you seen the revised integer protocols?
https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md

`SignedNumeric` will no longer refine `Comparable`. It will, however, still
require that you can define multiplication.

···

On Fri, Mar 31, 2017 at 11:09 AM, Sasha Lopoukhine via swift-evolution < swift-evolution@swift.org> wrote:

Hello everyone!

I'd like to discuss the constraint on Strideable. I see no reason to
restrict Strideable types to non-circular types. It would be pretty
neat/useful to be able to define a type such as a Week, with strides
looping around to the start of the enum. Currently, the only reason this is
impossible is because of the restriction on Stride that it must conform to
SignedNumber.

I think that it might be worth looking into defining a new protocol
specifically for the constraint on Strideable.Stride, something that looks
like this:

/// Instances of conforming types can be subtracted, arithmetically
/// negated, and initialized from `0`.
///
/// Axioms:
///
/// - `x - 0 == x`
/// - `-x == 0 - x`
/// - `-(-x) == x`
public protocol StrideProtocol : ExpressibleByIntegerLiteral {

    /// Returns the result of negating `x`.
    prefix public static func -(x: Self) -> Self

    /// Returns the difference between `lhs` and `rhs`.
    public static func -(lhs: Self, rhs: Self) -> Self
}

public typealias SignedNumber = StrideProtocol & Comparable

Strideable.Stride could then conform to StrideProtocol, allowing groups
etc.

WDYT?

-- Sasha

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Sasha Lopoukhine) #3

Completely forgot about those. Thank you. The point still stands, it doesn't really make sense to multiply strides by each other. I don't know how this would work, but if we're already ok to shake up the Integer protocols, then maybe adding one between SignedNumeric and SignedInteger might be ok.

···

On 31 Mar 2017, at 17:25, Xiaodi Wu <xiaodi.wu@gmail.com> wrote:

Have you seen the revised integer protocols?
https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md

`SignedNumeric` will no longer refine `Comparable`. It will, however, still require that you can define multiplication.

On Fri, Mar 31, 2017 at 11:09 AM, Sasha Lopoukhine via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
Hello everyone!

I'd like to discuss the constraint on Strideable. I see no reason to restrict Strideable types to non-circular types. It would be pretty neat/useful to be able to define a type such as a Week, with strides looping around to the start of the enum. Currently, the only reason this is impossible is because of the restriction on Stride that it must conform to SignedNumber.

I think that it might be worth looking into defining a new protocol specifically for the constraint on Strideable.Stride, something that looks like this:

/// Instances of conforming types can be subtracted, arithmetically
/// negated, and initialized from `0`.
///
/// Axioms:
///
/// - `x - 0 == x`
/// - `-x == 0 - x`
/// - `-(-x) == x`
public protocol StrideProtocol : ExpressibleByIntegerLiteral {

    /// Returns the result of negating `x`.
    prefix public static func -(x: Self) -> Self

    /// Returns the difference between `lhs` and `rhs`.
    public static func -(lhs: Self, rhs: Self) -> Self
}

public typealias SignedNumber = StrideProtocol & Comparable

Strideable.Stride could then conform to StrideProtocol, allowing groups etc.

WDYT?

-- Sasha

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Xiaodi Wu) #4

Completely forgot about those. Thank you. The point still stands, it
doesn't really make sense to multiply strides by each other. I don't know
how this would work, but if we're already ok to shake up the Integer
protocols, then maybe adding one between SignedNumeric and SignedInteger
might be ok.

The time for shaking up integer protocols is over, though. During that
time, it was considered whether separating Numeric into fields and rings
would be useful, which is what you're asking, but it was thought too
esoteric and rejected in favor of the approved design.

···

On Fri, Mar 31, 2017 at 11:31 AM, Sasha Lopoukhine <sasha.swi.evo@gmail.com> wrote:

On 31 Mar 2017, at 17:25, Xiaodi Wu <xiaodi.wu@gmail.com> wrote:

Have you seen the revised integer protocols?
https://github.com/apple/swift-evolution/blob/master/proposa
ls/0104-improved-integers.md

`SignedNumeric` will no longer refine `Comparable`. It will, however,
still require that you can define multiplication.

On Fri, Mar 31, 2017 at 11:09 AM, Sasha Lopoukhine via swift-evolution < > swift-evolution@swift.org> wrote:

Hello everyone!

I'd like to discuss the constraint on Strideable. I see no reason to
restrict Strideable types to non-circular types. It would be pretty
neat/useful to be able to define a type such as a Week, with strides
looping around to the start of the enum. Currently, the only reason this is
impossible is because of the restriction on Stride that it must conform to
SignedNumber.

I think that it might be worth looking into defining a new protocol
specifically for the constraint on Strideable.Stride, something that looks
like this:

/// Instances of conforming types can be subtracted, arithmetically
/// negated, and initialized from `0`.
///
/// Axioms:
///
/// - `x - 0 == x`
/// - `-x == 0 - x`
/// - `-(-x) == x`
public protocol StrideProtocol : ExpressibleByIntegerLiteral {

    /// Returns the result of negating `x`.
    prefix public static func -(x: Self) -> Self

    /// Returns the difference between `lhs` and `rhs`.
    public static func -(lhs: Self, rhs: Self) -> Self
}

public typealias SignedNumber = StrideProtocol & Comparable

Strideable.Stride could then conform to StrideProtocol, allowing groups
etc.

WDYT?

-- Sasha

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution