3 ms·
would that be similar to a throwing init in swift? struct NonEmptyAlphanumericString { let value: String init(string: String) throws { guard
by andrekandre 4y ago
would that be similar to a throwing init in swift?
struct NonEmptyAlphanumericString {
let value: String
init(string: String) throws {
guard !string.isEmpty else {
throw Errors.emptyString
}
guard string.isAlphanumeric else {
throw Errors.nonAlphanumericString
}
value = string
}
enum Errors: Error {
case nonAlphanumericString
case emptyString
}
}
- gavinray 4y agoSeems identical to me =) Though yours also has the benefit of typed errors I've always liked Swift, but never written it. It seems like Kotlin and Swift are the most two similar languages.
- andrekandre 4y agoyea i think its possible to express ideas from one to another in pretty straightforwards ways for sure