Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7008] Subclass existential doesn't satisfy generic class constraints #49556

Closed
karwa opened this issue Feb 15, 2018 · 1 comment
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@karwa
Copy link
Contributor

karwa commented Feb 15, 2018

Previous ID SR-7008
Radar None
Original Reporter @karwa
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 9.2

Apple Swift version 4.1 (swiftlang-902.0.34 clang-902.0.30)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 10ba50906ce8e750485046f8da2fb431

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

Issue Description:

Example:

class AClass {}
protocol AProto { var value: Int { get } }

class Box<T> { 
  private var _boxed: T
  init(_ b: T) { _boxed = b } 
}
extension Box where T: AClass { 
  var boxed: T { 
    get { return _boxed }
    set { _boxed = newValue } 
  } 
}

class One: AClass, AProto { var value: Int { return 1 } }
class Two: AClass, AProto { var value: Int { return 2 } }

let a: Box<AClass & AProto> = Box(One())
print(a.boxed) // error: 'AClass & AProto' is not convertible to 'AnyObject'
a.boxed = Two()
print(a.boxed)

let b: AClass & AProto = One()
print(b is AClass) // 'true'

See discussion at: https://forums.swift.org/t/confused-by-generic-behaviour-not-clear-if-it-is-a-bug/9823

@swift-ci
Copy link
Collaborator

Comment by Marc Palmer (JIRA)

I completely understand how Swift experts may say "you're holding it wrong" but I think there is a much wider and more important point here that this doesn't look wrong at all and hence any APIs built like this will be very hard for developers to reason about.

I am still struggling to internalise this nuance with existentials, and I've developed in many languages (C, C++, Object Pascal, ObjC, JS, 68000 ASM) for over 30 years!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants