[swift-evolution] 100% bikeshed topic: DictionaryLiteral
Charles Constant
charles at charlesism.com
Tue Jan 9 23:05:25 CST 2018
Oops. Kindly disregard my previous emails. Ben was kind enough to point out
to me that I was confusing dictionary literals with DictionaryLiteral
On Tue, Jan 9, 2018 at 8:56 PM, Ben Cohen <> wrote:
> Hi Charles,
>
> The naming issue strikes again :)
>
> Your code is making use of dictionary literals, not DictionaryLiteral. We
> are talking about the (clearly confusingly named!) DictionaryLiteral type,
> which is a type you can create from a dictionary literal. (See docs here:
> https://developer.apple.com/documentation/swift/dictionaryliteral)
>
> There’s no suggestion of getting rid of the ExpressibleByDictionaryLiteral
> capability.
>
> “Ceci n’est pas une literal”
> - Magritte
>
> On Jan 9, 2018, at 8:26 PM, Charles Constant via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Hi Nevin (et al)
>
> Here's the relevant section of my code. It's from a protocol I use called
> "ParameterSet" to extend OptionSets to contain small numbers (like an Enum
> with an associated type, but all the data is stored in the UInt). Wouldn't
> be my first choice to share, as it breaks KISS. Anyhow, the "
> ExpressibleByDictionaryLiteral" lets me specify a "params" template
> tersely. I only found out how to use the -Literal initializers last year. I
> love them.
>
> struct DrumNote: ParameterSet {
> let rawValue: UInt
>
> static let params: [ParamType] = [ ["velocity":0...255], "flam", "roll",
> "reverse" ]
>
> static let
> velocity = param(0),
> flam = param(1),
> roll = param(2),
> reverse = param(3),
>
> // ...
>
> }
>
> enum ParamType: ExpressibleByStringLiteral, ExpressibleByDictionaryLiteral
> {
> public init( dictionaryLiteral elements: (String,Any)... ) {
> if let pair = elements.first as? (String,ClosedRange<FloatLiteralType>) {
> self = .doub(pair.0,pair.1) }
> else if let pair = elements.first as? (String,CountableClosedRange<IntegerLiteralType>)
> { self = .int(pair.0, pair.1.lowerBound ... pair.1.upperBound ) }
> else { fatalError("Can only init from Double...Double or Int...Int") }
> }
>
> // ...
> }
>
>
> On Tue, Jan 9, 2018 at 6:10 PM, Nevin Brackett-Rozinsky <
> nevin.brackettrozinsky at gmail.com> wrote:
>
>> On Tue, Jan 9, 2018 at 7:47 PM, charles at charlesism.com <
>> charlesism.com at gmail.com> wrote:
>>
>>> I used a DictionaryLiteral only yesterday, and it turned what would have
>>> a typically unreadable array of Structs into something much more elegant.
>>> I'm pretty sure the only reason Literals (of all varieties) aren't used
>>> more often is because Swift programmers don't realize they are available
>>> and easy to implement.
>>>
>>
>> Could you provide an example of how you used DictionaryLiteral?
>>
>> Nevin
>>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
On Tue, Jan 9, 2018 at 8:26 PM, Charles Constant via swift-evolution <
swift-evolution at swift.org> wrote:
> Hi Nevin (et al)
>
> Here's the relevant section of my code. It's from a protocol I use called
> "ParameterSet" to extend OptionSets to contain small numbers (like an Enum
> with an associated type, but all the data is stored in the UInt). Wouldn't
> be my first choice to share, as it breaks KISS. Anyhow, the "
> ExpressibleByDictionaryLiteral" lets me specify a "params" template
> tersely. I only found out how to use the -Literal initializers last year. I
> love them.
>
> struct DrumNote: ParameterSet {
> let rawValue: UInt
>
> static let params: [ParamType] = [ ["velocity":0...255], "flam", "roll",
> "reverse" ]
>
> static let
> velocity = param(0),
> flam = param(1),
> roll = param(2),
> reverse = param(3),
>
> // ...
>
> }
>
> enum ParamType: ExpressibleByStringLiteral, ExpressibleByDictionaryLiteral
> {
> public init( dictionaryLiteral elements: (String,Any)... ) {
> if let pair = elements.first as? (String,ClosedRange<FloatLiteralType>) {
> self = .doub(pair.0,pair.1) }
> else if let pair = elements.first as? (String,CountableClosedRange<IntegerLiteralType>)
> { self = .int(pair.0, pair.1.lowerBound ... pair.1.upperBound ) }
> else { fatalError("Can only init from Double...Double or Int...Int") }
> }
>
> // ...
> }
>
>
> On Tue, Jan 9, 2018 at 6:10 PM, Nevin Brackett-Rozinsky <
> nevin.brackettrozinsky at gmail.com> wrote:
>
>> On Tue, Jan 9, 2018 at 7:47 PM, charles at charlesism.com <
>> charlesism.com at gmail.com> wrote:
>>
>>> I used a DictionaryLiteral only yesterday, and it turned what would have
>>> a typically unreadable array of Structs into something much more elegant.
>>> I'm pretty sure the only reason Literals (of all varieties) aren't used
>>> more often is because Swift programmers don't realize they are available
>>> and easy to implement.
>>>
>>
>> Could you provide an example of how you used DictionaryLiteral?
>>
>> Nevin
>>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20180109/86d5ac3c/attachment.html>
More information about the swift-evolution
mailing list