Swiftpack.co - UXJon/Kind as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by UXJon.
UXJon/Kind 1.0.3
A Kind<T> gives a quick and simple way to extensibly express a cascading kind that is only associated with a particular type T. Kinds have a string Id and can optionally fallback to another kind.
⭐️ 0
🕓 3 years ago
.package(url: "https://github.com/UXJon/Kind.git", from: "1.0.3")

Kind

A Kind<T> gives a quick and simple way to extensibly express a cascading kind that is only associated with a particular type T. Kinds have a string Id and can optionally fallback to another kind. This allows you to define something (e.g. a style) at various levels of specificity, and then use the most specific kind which is available.

Extensions can be used to define kinds for a particular type which can be accessed via dot-syntax.

As a specific example:

struct DragHandle {
	var kind:Kind<DragHandle> = .free
	///Other properties
}

extension Kind where T == DragHandle {
	static var free:Kind<DragHandle> = "general"
	static var rect:Kind<DragHandle> = .fallback("rect", .free)
	static var rectCorner:Kind<DragHandle> = .fallback("rectCorner", .rect)
	
	static var upperLeft:Kind<DragHandle> = .fallback("upperLeft",.rectCorner)
	//...
}

The value(in:) function can be used to get the most specific value associated with a kind in a given dictionary. It looks for the id in the given dictionary, then tries fallback ids if a value isn't found.

var styles:[Kind<DragHandle>: HandleStyle] = //...

let style:HandleStyle = kind.value(in: styles)

Only the Kind's id is considered in equality, the fallback is not considered. Two kinds with identical ids, but different fallbacks are considered equal.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics