[swift-evolution] [swift-users] Very strange automatic behavior between StringLiteralConvertible and pattern matching

David Hart david at hartbit.com
Tue Jan 5 11:30:11 CST 2016


Sorry about the double post.

> On 05 Jan 2016, at 18:26, David Hart via swift-users <swift-users at swift.org> wrote:
> 
> How is it that Swift allows code like this:
> 
> struct Sneaky: StringLiteralConvertible {
> 	init(stringLiteral value: String) {}
> 	init(extendedGraphemeClusterLiteral value: String) {}
> 	init(unicodeScalarLiteral value: String) {}
> }
> 
> func ~=(sneaky: Sneaky, string: String) -> Bool {
> 	return false
> }
> 
> enum NormalEnum: String {
> 	case Super = "super"
> 	case Mario = "mario"
> }
> 
> let value = NormalEnum(rawValue: "super”) // return nil!!!!
> 
> It hit completely by surprise today because of of a Regex library:
> 
> struct Regex: StringLiteralConvertible {
> 	init(stringLiteral value: String) {}
> 	init(extendedGraphemeClusterLiteral value: String) {}
> 	init(unicodeScalarLiteral value: String) {}
> 
> 	//...
> }
> 
> func ~=(regex: Regex, string: String) -> Bool {
> 	return regex.matches(string)
> }
> 
> If I was not already a Swift enthusiast, this behaviour would have left me completely dumbfounded.
> What can we do about it?
> 
> David.
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160105/a549144e/attachment.html>


More information about the swift-evolution mailing list