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-13238] feature request to treat implicitly unwrapped optional in protocol to be non optional #55679

Open
swift-ci opened this issue Jul 17, 2020 · 1 comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13238
Radar None
Original Reporter codelynx (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, LanguageFeatureRequest
Assignee None
Priority Medium

md5: e437e614ee8163f5a48d4882b4867434

Issue Description:

When I like to conform MyViewController to MyProtocol as follows. It causes error "Type 'MyViewController' does not conform to protocol 'MyProtocol'" in Swift 5

// code1.swift
class MyView: UIView {}

protocol MyProtocol: AnyObject {
  var myView: MyView { get }
}

// Type 'MyViewController' does not conform to protocol 'MyProtocol'
class MyViewController: UIViewController, MyProtocol {
  @IBOutlet var myView: MyView!
}

Of course, I have an option to write like this

// code2.swift
 class MyViewController: UIViewController, MyProtocol {
  @IBOutlet var _myView: MyView! 
  var myView: MyView { _myView }
}

Or, I can make its accessor to be implicitly unwrapped optionals as this.

// code3.swift
class MyView: UIView {}

protocol MyProtocol: AnyObject { 
  var myView: MyView! { get }
}

class MyViewController: UIViewController, MyProtocol {
  @IBOutlet var myView: MyView!
}

If the protocol is written by ourself, we can rewrite as code3, however, if the protocol is provided by third party, then I have take other way such as code2.

I have a feature request that return value of implicitly unwrapped optional value to be treated as none optional value (code1). Or, if swift team come up with better way to solve this problem, then I would be happy.

Thanks,

@theblixguy
Copy link
Collaborator

var MyView and MyView to be equivalent as long as the implicit unwrap succeeds, but what happens when it doesn’t? The protocol requires conforming types to always provide a valid MyView value but it’s possible for the implicit unwrap to fail and nil is not a valid value.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement
Projects
None yet
Development

No branches or pull requests

2 participants