Okay, i was able to get the correct refcounting behavior with the class directly in the enum payload, but this slightly more realistic example, where the class is wrapped in a struct doesnt:
final
class C
{
init()
{
}
}
struct S
{
var c:C
init()
{
self.c = .init()
}
mutating
func foo()
{
print(isKnownUniquelyReferenced(&self.c))
}
}
enum E
{
case empty
case s(S)
}
func foo()
{
var e:E = .s(.init())
switch e
{
case .s(var s):
e = .empty
s.foo()
default:
break
}
}
foo()
the assignments that happen inside the switch
still work as expected:
func foo()
{
var e:E = .empty
switch e
{
case .s:
break
default:
var s:S = .init()
s.foo()
e = .s(s)
s.foo()
e = .empty
s.foo()
}
}
// true
// false
// true
what i really don’t get is why this, structurally identical code using Optional
instead of E
works but my first example doesn’t:
func foo()
{
var e:S? = .some(.init())
switch e
{
case .some(var s):
e = nil
s.foo()
case .none:
break
}
}