[swift-evolution] Proposal: 'T(literal)' should construct T using the appropriate literal protocol if possible

Ben Rimmington me at benrimmington.com
Fri Jun 3 19:31:53 CDT 2016

John McCall wrote:

> I think that's a very promising way of thinking about literals.  Writing
> a literal creates a notional value whose type is the informal, infinite-
> precise type of all integer/FP/collection/etc. literals, which (1) can be
> implicitly converted to any type that implements the appropriate protocol
> and (2) in fact *must* be converted to some such type (possibly the
> default type for that literal) in order for the code to be executable.

Could you allow IntegerLiteralConvertible.IntegerLiteralType associatedtype
to override the default Swift.IntegerLiteralType typealias iff there's more
than one unlabelled init(_:) to choose from? Then you can call the "correct"
init(_:) instead of calling init(integerLiteral:) as a "special case".


    /// Conforming types can be initialized with integer literals.
    public protocol IntegerLiteralConvertible {
        associatedtype IntegerLiteralType : _BuiltinIntegerLiteralConvertible
        /// Create an instance initialized to `value`.
        init(integerLiteral value: IntegerLiteralType)


    /// The default type for an otherwise-unconstrained integer literal.
    public typealias IntegerLiteralType = Int

-- Ben

More information about the swift-evolution mailing list