[swift-evolution] Proposal: conversion protocol naming conventions

L Mihalkovic laurent.mihalkovic at gmail.com
Thu Apr 14 10:18:40 CDT 2016


I was reading the document and was wondering if it might make sense to look into using definitions rather than conventions:

public protocol LiteralCreatable {
    associatedtype LiteralType
    init(literalValue : LiteralType)
}
public protocol NilLiteralConvertible : LiteralCreatable {
    associatedtype LiteralType = ()
}
public protocol StringLiteralCreatable : LiteralCreatable {
    associatedtype LiteralType = String
}
public protocol IntLiteralCreatable : LiteralCreatable {
    associatedtype LiteralType = Int
}

…

which leads to the following code:

struct  TT { }
extension TT: IntLiteralCreatable {
    init(literalValue: Int) { }
}
extension TT: StringLiteralCreatable {
    init(literalValue: String) { }
}
let tt1 = TT()
let tt2 = TT(literalValue: 20)
let tt3 = TT(literalValue: "StringLiteral")

…

or even this:

struct  XX { }
extension XX: IntLiteralCreatable, StringLiteralCreatable {
    init(literalValue: Int) { }
    init(literalValue: String) { }
}
let xx1 = XX()
let xx2 = XX(literalValue: 20)
let xx3 = XX(literalValue: "StringLiteral")

cheers
LM/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160414/c02bd8fa/attachment.html>


More information about the swift-evolution mailing list