XPKit

THE 用于在 Swift 中进行 Exceptional Programming 的框架

什么是 Exceptional Programming

函数看起来像这样

func name(...) throws -> Never {
	...
}

导入

import XPKit
dependencies: [
	.package(url: "https://github.com/ExceptionalProgramming/XP-Swift",
	         from: "1.0.0")
],
targets: [
	.target(
		name: "",
		dependencies: [
			"XPKit"
		]),
]

异常

异常是一个应该被 thrownclass (类)

所有 do/catch 代码块都应该重新抛出任何未处理的异常

...
} catch let error { throw error }

main()

do {
	try ...
} catch _ as ProgramTerminated {
} catch let error { throw error }

获取值

我们理解并非所有代码都是异常的,因此我们提供了 get<T>(from:) -> T 函数,它将从抛出的 ValueException<T> 中提取值

加法

提供了一个 add<T: Addable> 函数,它 throws 所有值的总和。IntUInt 类型族都遵循 Addable 协议