Is `$` an identifier character now?

The Identifiers section of the Swift language reference do not seem to allow identifiers such as a$b, but I can currently type

let a$b = 4

Is this intended? There's no reference to U+0024 (dollar sign) and, following the specification, $ should be only used for implicit parameter names and property wrappers' projections.

5 Likes

Filed https://bugs.swift.org/browse/SR-13981.

2 Likes

This is not a bug, even before property wrappers were a thing you could write let _$foo which compiled just fine. Removing it now is a breaking change.

If anything, the documentation should be updated.

Possibly relevant history: SE–0144 “Allow Single Dollar Sign as a Valid Identifier” (rejected)

1 Like

Possibly related (and the source of this finding on my side): in the Alternatives Considered section of SE-0258 there's this bit

Looks like the proposal assumed that identifiers with $ not at the starting position were "safe".

IIRC this was meant as an infix operator to chain certain members similar to the dot.

foo.bar
foo$bar

The ship for that has sailed and the issue of ambiguity may or may not have been discussed during any of the past reviews.

I still have code that uses the $ in places where the compiler can‘t generate property wrappers automatically.

This is interesting, can you provide an example?

Basically manually written code like this:

var _foo = Binding<Int>(...)
var foo: Int {
  get {
    _foo.wrappedValue
  }
  set {
    _foo.wrappedValue = newValue
  }
}

var _$foo: Binding<Int> {
  _foo.projectedValue
}

I will refactor this when we officially have property wrapper support for local variables.

1 Like
Terms of Service

Privacy Policy

Cookie Policy