dhoepfl
(Daniel Höpfl)
1
I compiled swift for Debian (LLVM f63b283c71, Clang 41ac4c4262, Swift 7cb2c9bbba) and get a strange error/warning-pair with for let/var in:
%> cat l.swift
import Foundation
func f(_ data: Data) -> Void {
for let x in data {
print("\(x)")
}
}
%> swift l.swift
l.swift:4:8: error: 'let' pattern cannot appear nested in an already immutable context
for let x in data {
^
If it is an immutable context, then why not allow let? But ok, let’s use var:
%> cat v.swift
import Foundation
func f(_ data: Data) -> Void {
for var x in data {
print("\(x)")
}
}
%> swift v.swift
v.swift:4:12: warning: variable 'x' was never mutated; consider changing to 'let' constant
for var x in data {
~~~~^
There is no warning for var in REPL:
%> swift
Welcome to Swift version 5.0-dev (LLVM f63b283c71, Clang 41ac4c4262, Swift 7cb2c9bbba).
Type :help for assistance.
1> import Foundation
2.
3. func f(_ data: Data) -> Void {
4. for var x in data {
5. print("\(x)")
6. }
7. }
8>
1 Like
sveinhal
(Svein Halvor Halvorsen)
2
for x in data {
print("\(x)")
}
Martin
(Martin R)
3
So the diagnostic message is misleading, perhaps that can be improved? Xcode on macOS presents the same warning, but the “Fix-it” (correctly) removes the var .
4 Likes
Martin
(Martin R)
5
Which Swift version are you using exactly? I cannot yet reproduce the warning on Linux.
dhoepfl
(Daniel Höpfl)
6
Will do.
5.0-dev
(LLVM f63b283c71, Clang 41ac4c4262, Swift 7cb2c9bbba)
Martin
(Martin R)
7
That was my fault. I had tried it in the REPL and not – as you clearly wrote – by calling swift v.swift. The REPL does not show a warning, but calling swift with a source file argument does.