hartbit
(David Hart)
January 5, 2016, 5:28pm
1
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.
Joe_Groff
(Joe Groff)
January 6, 2016, 7:05pm
2
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!!!!
I see two bugs here. When an enum has a raw value type, the compiler generates this initializer:
init(rawValue: String) {
switch rawValue {
case "super":
self = .Super
...
}
}
so uses ~= pattern matching to match the raw value. It would be more sensible to always use `==` comparison in the synthesized initializer. However, I'm surprised too that the type checker favors ~=(Sneaky, String) over ~=(String, String); it should at best be ambiguous. Do you have time to file these two bugs?
-Joe
···
On Jan 5, 2016, at 9:28 AM, David Hart via swift-users <swift-users@swift.org> wrote:
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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
hartbit
(David Hart)
January 6, 2016, 10:31pm
3
I can file those bugs. Would it be beneficial if I also created failing unit tests?
David.
···
On 06 Jan 2016, at 20:05, Joe Groff <jgroff@apple.com> wrote:
On Jan 5, 2016, at 9:28 AM, David Hart via swift-users <swift-users@swift.org <mailto:swift-users@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!!!!
I see two bugs here. When an enum has a raw value type, the compiler generates this initializer:
init(rawValue: String) {
switch rawValue {
case "super":
self = .Super
...
}
}
so uses ~= pattern matching to match the raw value. It would be more sensible to always use `==` comparison in the synthesized initializer. However, I'm surprised too that the type checker favors ~=(Sneaky, String) over ~=(String, String); it should at best be ambiguous. Do you have time to file these two bugs?
-Joe
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@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users
Joe_Groff
(Joe Groff)
January 6, 2016, 10:41pm
4
Definitely. Thanks for uncovering these issues.
-Joe
···
On Jan 6, 2016, at 2:31 PM, David Hart <david@hartbit.com> wrote:
I can file those bugs. Would it be beneficial if I also created failing unit tests?
David.