Comments (2)
It appears you're right that the Swift compiler accepts case one(_ x: Int)
so we should probably fix this in the TM grammar. But why would someone want this? Is it any different from case one(Int)
? In fact, the language grammar doesn't seem to mention that the former is allowed.
Relatedly, it seems there is currently a bug/limitation where
let x: Foo = .four(x: 4) // valid
let y: Foo = .four(4) // invalid
but
let z = Foo.four
z(x: 4) // invalid
z(4) // valid
I think this might be what SE-0155 is trying to fix 🤷♂
from swift.tmbundle.
Thanks for your response!
case one(_ x: Int)
is not common, but I’ve seen members of my team use that syntax because it documents the associated value’s purpose without requiring the call site to use the name.
For example, if we have an enum that represents rows in a table view:
enum Row {
case itemCount(Int)
case subscription(_ plansDescription: String)
}
The case name case itemCount(Int)
is descriptive enough to omit an associated value label. However, case subscription(String)
by itself would make it difficult to determine what the String
is used for. On the other hand, case subscription(plansDescription: String)
would make the call site more onerous:
let plansDescription = ... // complex, multiline logic
// `case subscription(plansDescription: String)` has duplication:
let row = Row.subscription(plansDescription: plansDescription)
// `case subscription(_ plansDescription: String)` has no duplication:
let row = Row.subscription(plansDescription)
It looks like your example with four
isn’t something SE-0155 was meant to address. Based on an example in the “Proposed Solution” section, z(x: 4)
would still be invalid:
let f = Expr.elet
f([], anExpr) // valid, just like `z(4)`
f(locals: [], body: anExpr) // invalid, just like `z(x: 4)`
The root of the error in the z(x: 4)
example is that function variables can’t have argument labels (other than _
). Your example behaves exactly the same with a function as it does with an enum case constructor:
func divide(dividend: Double, divisor: Double) -> Double {
return dividend / divisor
}
let x2 = divide(dividend: 1.0, divisor: 2.0) // valid
let y2 = divide(1.0, 2.0) // invalid
let z2 = divide
z2(dividend: 1.0, divisor: 2.0) // error: extraneous argument labels 'dividend:divisor:' in call
z2(1.0, 2.0) // valid
If we try to give argument labels to a function variable, the compiler forces us to use _
:
let a: (dividend: Double, divisor: Double) -> Double = divide // error: function types cannot have argument labels; use '_' before 'dividend'; use '_' before 'divisor'
let b: (_ dividend: Double, _ divisor: Double) -> Double = divide // valid
b(dividend: 1.0, divisor: 2.0) // error: extraneous argument labels 'dividend:divisor:' in call
b(1.0, 2.0) // valid
Ideally, the syntax highlighting would mark dividend
and divisor
as invalid in let a: (dividend: Double, divisor: Double) -> Double = divide
, too. Of course, that’s less important than marking case one(_ x: Int)
as invalid.
from swift.tmbundle.
Related Issues (13)
- Not Appearing in TM2 Latest Release HOT 3
- Auto-completion and documentation look-up HOT 2
- Incorrect highlighting of unclosed ` in comment HOT 1
- Floating-Point Literals not Parsing Correctly HOT 6
- Numeric literals don't work well in GitHub linguist HOT 1
- Highlighting issue after associated type with no default HOT 3
- Incorrect syntax highlighting HOT 13
- Complex sequences break highlighting HOT 1
- Bundle Does Not Work In Atom & Sublime Text HOT 5
- Wrong color for parameter named "continue" HOT 3
- Support for raw string literals HOT 11
- Add support for SE-0354 Regex Literals HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift.tmbundle.