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

ParsePattern: allow identifiers starting with $ in protocol declaration. #63054

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ashdnazg
Copy link
Contributor

@ashdnazg ashdnazg commented Jan 16, 2023

This enables protocols to require the existence of certain projected values in conforming types:

protocol ProtocolWithProjectedValue {
  var foo: Int { get set }
  var $foo: Wrapper<Int> { get }
}

struct StructWithPropertyWrapper: ProtocolWithProjectedValue {
  @Wrapper var foo = 1337
}

It is mentioned in the comments here: https://forums.swift.org/t/property-wrapper-requirements-in-protocols/33953 but that proposal seems to be stuck.
As opposed to the full proposal, this PR is a low hanging fruit (one line of code changed) that gives almost the same new capabilities to the developer.
From the desugaring point of view, this PR can also be seen as a prerequisite of the aforementioned proposal.

This enables protocols to require the existence of certain projected
values in conforming types.
@Sajjon
Copy link

Sajjon commented Jan 17, 2023

Yay! This will solve my problem https://stackoverflow.com/q/57652925

@farzadshbfn
Copy link

I am not part of the swift team, but out of curiosity, but looking at the change, it seems it allows definition of variables with $ prefix inside protocols. I think this change requires more than just enabling it inside protocols, custom errors generated also should be clear. For example in following cases:

protocol ProtocolWithDollarSignProperty {
    var foo: String { get } 
    var $foo: Wrapper<Int> { get }
}

This protocol can never be satisfied by any type trying to implement it (as the foo type is different from Wrapper generic type). I believe, language should prohibit introduction of such codes.

@ashdnazg
Copy link
Contributor Author

ashdnazg commented Jan 17, 2023

It is actually possible, even if a bit insane:

@propertyWrapper
struct InsaneWrapper<Value> {
  var wrappedValue: Value

  var projectedValue: Wrapper<Int> {
    .init(wrappedValue: 5)
  }
}

struct ConformingStruct: ProtocolWithDollarSignProperty {
  @InsaneWrapper var foo: String
}

Property wrappers seem to allow you to project arbitrary values, the only thing we constrain in the protocol is the type of the projected value, not its meaning.

@theblixguy theblixguy added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Jan 17, 2023
@rakuyoMo
Copy link

This feature is very much needed and this feature is very useful for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants