pointfreeco / swift-nonempty

🎁 A compile-time guarantee that a collection contains a value.

Home Page:https://www.pointfree.co/episodes/ep20-nonempty

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conformance to the `ExpressibleByStringLiteral` makes it possible to create dangerous situation

letko-dmitry opened this issue Β· comments

It looks like conforming to the ExpressibleByStringLiteral protocol isn't safe and leads to the following use case:

let a: NonEmptyString = ""

And obviously it is a silenced crash.
The same for all the other initializators like this.

As far as I see, it was discussed before there: #48 (comment). Don't you think that avoiding cases like the one above is the exact reason of having such a solution?

@letko-dmitry This is working as intended and holds true for ExpressibleByArrayLiteral, where we have this explicit precondition:

precondition(!elements.isEmpty, "Can't construct \(Self.self) from empty array literal")

We consider this kind of assignment to be a programmer error, since literals are only ever added by programmers working on the project.

That said, we'd be open to introducing more explicit preconditions in the string initializers that better describe the problem, as they exist in the array literal conformance above.

We'd also be open to exploring how macros may solve for this at compile-time, e.g.:

#nonEmpty("")
          ┬─ 
          β•° πŸ›‘ Cannot use empty literal with '#nonEmpty'

Because we don't consider this a bug in the library, I'm going to convert it to a discussion.