xx-li
(Stellar)
September 6, 2018, 8:20am
1
class Test {
func test() {
struct NestedTest: Comparable {
static func < (lhs: NestedTest, rhs: NestedTest) -> Bool {
return false;
}
static func == (lhs: NestedTest, rhs: NestedTest) -> Bool {
return false;
}
}
}
}
error: Type 'NestedTest' does not conform to protocol 'Comparable'
Is it a bug or does not support this?
The fourth time this is asked about this week; apparently a recently introduced issue. Here's the SR-8696
(I've only tested this in Xcode 10 b6)
This program compiles and works as expected:
struct S {
var v: Int
static func +(lhs: S, rhs: S) -> S { return S(v: lhs.v + rhs.v) }
}
func foo() {
print(S(v: 123) + S(v: 456)) // 579
}
foo()
But this program produces an unexpected compile time error:
func foo() {
struct S {
var v: Int
static func +(lhs: S, rhs: S) -> S { return S(v: lhs.v + rhs.v) }
}
print(S(v: 123) + S(v: 456)) // ERROR: Binary operator '+' can…
why does this not work when the struct is local to a function?
func f()
{
struct Pair:Hashable, Comparable
{
let x:Int, y:Int
static
func < (lhs:Pair, rhs:Pair) -> Bool
{
if lhs.y < rhs.y
{
return true
}
else if lhs.y == rhs.y
{
return lhs.x < rhs.x
}
else
{
return false
}
}
…
Hello ! I would like to have some clarifications about why Set does not interact properly with a Hashable struct declared in a function.
Here I have a struct Hello that conforms to hashable and only needs its id for the hashValue. Because structs need to have Equatable custom conformance to have custom hash behavior, so a == function is declared. 23
I first declare a Set with an identifier 123 and the I try to insert another struct having the same identifier with different "value". It does not…
2 Likes
xx-li
(Stellar)
September 6, 2018, 2:01pm
4
Thank you for your answer. I want to try the sample you provided, but can't compile?How to get the output you gave?
why wasn’t this caught in the source compatibility suite?
Types nested in functions that explicitly conform to protocols with an operator function requirement is quite the specific case, I'm not surprised there isn't a compiler test for that. Regarding the source compatibility suite, it could be that the changes in the PR that introduced the bug didn't require testing source compatibility. But I can't be sure there is an appropriate test in the source compatibility suite either; that might also be the case, assuming there is high probability that the source compatibility tests were run at least once after introducing the bug.
jrose
(Jordan Rose)
September 6, 2018, 4:45pm
7
I'm pretty sure there's no test for it because it has never worked. I'm not sure why it's getting so much attention right now.