Jens
1
func perhapsConfusing() {
let s =
"Since this function compiles fine. One might "
"think that string literals in Swift can be "
"written like this. But that is not the case. "
"Only the first string literal will be printed."
"The rest (including eg this) will be silently "
"ignored."
12.34 // As will this.
12 + 4 // And this.
123 == 456 // And this.
print(s) // Prints: "Since this function compiles fine. One might "
}
Besides being possibly surprising for newcomers (who may assume string
literals split across several lines as above will be joined), there are
some related inconsistencies in whether warnings are generated for unused
results or not, for example
123 == 456 // Unused result is silently ignored, while
1.2 == 3.4 // produces a warning: Result of call to '==' is unused.
(The following bug report is about unused results, but it doesn't mention
the IMHO similar unused-literals-case: [SR-245] Missing @warn_unused_result attributes in Standard Library · Issue #42867 · apple/swift · GitHub
)
So my question is:
Should/could there be a warning for unused literals?
/Jens
I think that there should be a warning for unused literals. I can't think of any case where you would like to have a literal just hanging there in Swift.
Félix
···
Le 1 févr. 2016 à 06:15:32, Jens Persson via swift-evolution <swift-evolution@swift.org> a écrit :
func perhapsConfusing() {
let s =
"Since this function compiles fine. One might "
"think that string literals in Swift can be "
"written like this. But that is not the case. "
"Only the first string literal will be printed."
"The rest (including eg this) will be silently "
"ignored."
12.34 // As will this.
12 + 4 // And this.
123 == 456 // And this.
print(s) // Prints: "Since this function compiles fine. One might "
}
Besides being possibly surprising for newcomers (who may assume string literals split across several lines as above will be joined), there are some related inconsistencies in whether warnings are generated for unused results or not, for example
123 == 456 // Unused result is silently ignored, while
1.2 == 3.4 // produces a warning: Result of call to '==' is unused.
(The following bug report is about unused results, but it doesn't mention the IMHO similar unused-literals-case: [SR-245] Missing @warn_unused_result attributes in Standard Library · Issue #42867 · apple/swift · GitHub )
So my question is:
Should/could there be a warning for unused literals?
/Jens
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
jrose
(Jordan Rose)
3
Yep. I'd just consider this a bug, though someone from the standard library would have to confirm that they don't consider it an API change. Feel free to tack it on to SR-245; we might as well do them all at the same time.
Jordan
···
On Feb 1, 2016, at 9:44, Félix Cloutier via swift-evolution <swift-evolution@swift.org> wrote:
I think that there should be a warning for unused literals. I can't think of any case where you would like to have a literal just hanging there in Swift.
Félix
Le 1 févr. 2016 à 06:15:32, Jens Persson via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :
func perhapsConfusing() {
let s =
"Since this function compiles fine. One might "
"think that string literals in Swift can be "
"written like this. But that is not the case. "
"Only the first string literal will be printed."
"The rest (including eg this) will be silently "
"ignored."
12.34 // As will this.
12 + 4 // And this.
123 == 456 // And this.
print(s) // Prints: "Since this function compiles fine. One might "
}
Besides being possibly surprising for newcomers (who may assume string literals split across several lines as above will be joined), there are some related inconsistencies in whether warnings are generated for unused results or not, for example
123 == 456 // Unused result is silently ignored, while
1.2 == 3.4 // produces a warning: Result of call to '==' is unused.
(The following bug report is about unused results, but it doesn't mention the IMHO similar unused-literals-case: https://bugs.swift.org/browse/SR-245 )
So my question is:
Should/could there be a warning for unused literals?
/Jens
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
nnnnnnnn
(Nate Cook)
5
Yep. I'd just consider this a bug, though someone from the standard library would have to confirm that they don't consider it an API change. Feel free to tack it on to SR-245; we might as well do them all at the same time.
Jordan
I think that there should be a warning for unused literals. I can't think of any case where you would like to have a literal just hanging there in Swift.
Félix
I.... do this all the time in Playgrounds, mainly to see if a conditional branch has been executed as I'm testing or building:
But a warning is a good idea otherwise, and I can figure something else out. :)

···
On Feb 1, 2016, at 3:14 PM, Jordan Rose via swift-evolution <swift-evolution@swift.org> wrote:
On Feb 1, 2016, at 9:44, Félix Cloutier via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
Le 1 févr. 2016 à 06:15:32, Jens Persson via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :
func perhapsConfusing() {
let s =
"Since this function compiles fine. One might "
"think that string literals in Swift can be "
"written like this. But that is not the case. "
"Only the first string literal will be printed."
"The rest (including eg this) will be silently "
"ignored."
12.34 // As will this.
12 + 4 // And this.
123 == 456 // And this.
print(s) // Prints: "Since this function compiles fine. One might "
}
Besides being possibly surprising for newcomers (who may assume string literals split across several lines as above will be joined), there are some related inconsistencies in whether warnings are generated for unused results or not, for example
123 == 456 // Unused result is silently ignored, while
1.2 == 3.4 // produces a warning: Result of call to '==' is unused.
(The following bug report is about unused results, but it doesn't mention the IMHO similar unused-literals-case: https://bugs.swift.org/browse/SR-245 )
So my question is:
Should/could there be a warning for unused literals?
/Jens
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
jrose
(Jordan Rose)
6
Playgrounds automatically suppress unused result warnings, exactly because of this. (Admittedly that can be probematic in the other direction, but still.)
Jordan
···
On Feb 2, 2016, at 18:42 , Nate Cook <natecook@gmail.com> wrote:
On Feb 1, 2016, at 3:14 PM, Jordan Rose via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
Yep. I'd just consider this a bug, though someone from the standard library would have to confirm that they don't consider it an API change. Feel free to tack it on to SR-245; we might as well do them all at the same time.
Jordan
On Feb 1, 2016, at 9:44, Félix Cloutier via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
I think that there should be a warning for unused literals. I can't think of any case where you would like to have a literal just hanging there in Swift.
Félix
I.... do this all the time in Playgrounds, mainly to see if a conditional branch has been executed as I'm testing or building:
<PastedGraphic-1.png>
But a warning is a good idea otherwise, and I can figure something else out. :)