Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpressibleByArrayLiteral and ExpressibleByDictionaryLiteral conformances #48

Closed
fwcd opened this issue Mar 13, 2022 · 2 comments · Fixed by #49
Closed

ExpressibleByArrayLiteral and ExpressibleByDictionaryLiteral conformances #48

fwcd opened this issue Mar 13, 2022 · 2 comments · Fixed by #49

Comments

@fwcd
Copy link
Contributor

fwcd commented Mar 13, 2022

It would be very convenient to have these conformances, since non-empty collections could be initialized as naturally as the built-in collection types:

let x: NonEmpty<[Int]> = [1, 2, 3]
let y: NonEmpty<[Int: String]> = [1: "abc", 2: "def"]

However, since ExpressibleByArrayLiteral and ExpressibleByDictionaryLiteral don't allow their initializers to fail, we would have to crash at runtime, which might not be desirable given the description of the library as a compile-time guarantee:

init(arrayLiteral: Element...) {
  guard !arrayLiteral.isEmpty else { fatalError("Non-empty array literal!") }
  self.init(rawValue: arrayLiteral)!
}

or

init(arrayLiteral: Element...) {
  assert(!arrayLiteral.isEmpty, "Non-empty array literal!")
  self.init(rawValue: arrayLiteral)!
}

Thoughts?

@stephencelis
Copy link
Member

@fwcd This sounds totally reasonable to us! Want to draft a PR implementing the functionality?

@fwcd
Copy link
Contributor Author

fwcd commented Mar 15, 2022

Sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants