[swift-evolution] TreeLiteralConvertible

Milos Rankovic milos at milos-and-slavica.net
Thu Apr 14 16:03:00 CDT 2016


> On 14 Apr 2016, at 21:36, John McCall <rjmccall at apple.com> wrote:
> 
> No, you just need Tree to conform to both ArrayLiteralConvertible and IntegerLiteralConvertible, and it implements the latter by building a Value out of it.

You mean this:

public enum IntTree {
	case Leaf(Int)
	case Branches([IntTree])
}

extension IntTree : ArrayLiteralConvertible {
	public init(arrayLiteral elements: IntTree...) {
		self = .Branches(elements)
	}
}

extension IntTree : IntegerLiteralConvertible {
	public init(integerLiteral value: IntegerLiteralType) {
		self = .Leaf(value)
	}
}

let tree: IntTree = [[], 1, [2, 3], [[4, 5], [6, 7], [8, 9]]]

> you'll simply have to make your Tree less generic


Yep, that’s the rub… With generic trees you can express yourself freely, whether you feel like:

import SpriteKit

let actionTree: Tree<SKAction> = [
	◊.waitForDuration(1),
	[
		◊.fadeInWithDuration(1),
		◊.scaleTo(1, duration: 1)
	],
	◊.playSoundFileNamed("TaDa", waitForCompletion: false)
]

… or:

let johnny: DictionaryTree<String, JSONValue> =
[
	"name": ◊"Johnny Appleseed",
	"age": ◊25,
	"address": [
		"house_number": ◊21,
		"street": ◊"2nd Street",
		"city": ◊"New York"
	]
]

I’d just love to get rid of that prefix operator…

milos









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


More information about the swift-evolution mailing list