I'm not sure if there's a more appropriate place to ask this question, but I figured at the very least I could get pointed in the right direction. I've tried searching online and haven't been able to find anything addressing this.
I was trying to use the `defer` statement in a Playground, and was surprised to find that it never prints anything in the preview pane on the side. I was expecting the evaluation of the code in the `defer` statement to show up in line with the statements, even though they're executed after the last line in the function. I made a very simple playground that modifies a global variable and prints the value in the `defer` statement, and when I print the global variable after calling my function it shows the correct updated value, so the code in the `defer` statement is getting run as expected. Here's my sample code with the Playground output in comments on the side:
var x = 3 // 3
func doSomething() {
print(1) // "1\n"
defer {
x += 1
print(x)
}
print(2) // "2\n"
}
doSomething()
print(x) // "4\n"
On Aug 4, 2017, at 12:41, Joe DeCapo via swift-users <swift-users@swift.org> wrote:
Hi everyone,
I'm not sure if there's a more appropriate place to ask this question, but I figured at the very least I could get pointed in the right direction. I've tried searching online and haven't been able to find anything addressing this.
I was trying to use the `defer` statement in a Playground, and was surprised to find that it never prints anything in the preview pane on the side. I was expecting the evaluation of the code in the `defer` statement to show up in line with the statements, even though they're executed after the last line in the function. I made a very simple playground that modifies a global variable and prints the value in the `defer` statement, and when I print the global variable after calling my function it shows the correct updated value, so the code in the `defer` statement is getting run as expected. Here's my sample code with the Playground output in comments on the side:
var x = 3 // 3
func doSomething() {
print(1) // "1\n"
defer {
x += 1
print(x)
}
print(2) // "2\n"
}
doSomething()
print(x) // "4\n"
On Aug 4, 2017, at 12:41, Joe DeCapo via swift-users <swift-users@swift.org> wrote:
Hi everyone,
I'm not sure if there's a more appropriate place to ask this question, but I figured at the very least I could get pointed in the right direction. I've tried searching online and haven't been able to find anything addressing this.
I was trying to use the `defer` statement in a Playground, and was surprised to find that it never prints anything in the preview pane on the side. I was expecting the evaluation of the code in the `defer` statement to show up in line with the statements, even though they're executed after the last line in the function. I made a very simple playground that modifies a global variable and prints the value in the `defer` statement, and when I print the global variable after calling my function it shows the correct updated value, so the code in the `defer` statement is getting run as expected. Here's my sample code with the Playground output in comments on the side:
var x = 3 // 3
func doSomething() {
print(1) // "1\n"
defer {
x += 1
print(x)
}
print(2) // "2\n"
}
doSomething()
print(x) // "4\n"
Either way is fine. I suspect this is a bug with the transformation the Swift compiler performs when compiling for a playground, rather than with the machinery that goes with running the playground, but I haven’t looked into it.
Jordan
···
On Aug 7, 2017, at 13:22, Joe DeCapo <snoogansbc@gmail.com> wrote:
Since it's Playground bug, should I file it in Apple's bug reporter rather than Swift's?
On Aug 7, 2017, at 3:12 PM, Jordan Rose <jordan_rose@apple.com> wrote:
On Aug 4, 2017, at 12:41, Joe DeCapo via swift-users <swift-users@swift.org> wrote:
Hi everyone,
I'm not sure if there's a more appropriate place to ask this question, but I figured at the very least I could get pointed in the right direction. I've tried searching online and haven't been able to find anything addressing this.
I was trying to use the `defer` statement in a Playground, and was surprised to find that it never prints anything in the preview pane on the side. I was expecting the evaluation of the code in the `defer` statement to show up in line with the statements, even though they're executed after the last line in the function. I made a very simple playground that modifies a global variable and prints the value in the `defer` statement, and when I print the global variable after calling my function it shows the correct updated value, so the code in the `defer` statement is getting run as expected. Here's my sample code with the Playground output in comments on the side:
var x = 3 // 3
func doSomething() {
print(1) // "1\n"
defer {
x += 1
print(x)
}
print(2) // "2\n"
}
doSomething()
print(x) // "4\n"
Ok, cool. Thanks for the response. I'll file it in the Swift bug tracker.
···
On Aug 7, 2017, at 3:23 PM, Jordan Rose <jordan_rose@apple.com> wrote:
Either way is fine. I suspect this is a bug with the transformation the Swift compiler performs when compiling for a playground, rather than with the machinery that goes with running the playground, but I haven’t looked into it.
Jordan
On Aug 7, 2017, at 13:22, Joe DeCapo <snoogansbc@gmail.com> wrote:
Since it's Playground bug, should I file it in Apple's bug reporter rather than Swift's?
On Aug 7, 2017, at 3:12 PM, Jordan Rose <jordan_rose@apple.com> wrote:
On Aug 4, 2017, at 12:41, Joe DeCapo via swift-users <swift-users@swift.org> wrote:
Hi everyone,
I'm not sure if there's a more appropriate place to ask this question, but I figured at the very least I could get pointed in the right direction. I've tried searching online and haven't been able to find anything addressing this.
I was trying to use the `defer` statement in a Playground, and was surprised to find that it never prints anything in the preview pane on the side. I was expecting the evaluation of the code in the `defer` statement to show up in line with the statements, even though they're executed after the last line in the function. I made a very simple playground that modifies a global variable and prints the value in the `defer` statement, and when I print the global variable after calling my function it shows the correct updated value, so the code in the `defer` statement is getting run as expected. Here's my sample code with the Playground output in comments on the side:
var x = 3 // 3
func doSomething() {
print(1) // "1\n"
defer {
x += 1
print(x)
}
print(2) // "2\n"
}
doSomething()
print(x) // "4\n"
On Aug 7, 2017, at 3:33 PM, Joe DeCapo via swift-users <swift-users@swift.org> wrote:
Ok, cool. Thanks for the response. I'll file it in the Swift bug tracker.
On Aug 7, 2017, at 3:23 PM, Jordan Rose <jordan_rose@apple.com> wrote:
Either way is fine. I suspect this is a bug with the transformation the Swift compiler performs when compiling for a playground, rather than with the machinery that goes with running the playground, but I haven’t looked into it.
Jordan
On Aug 7, 2017, at 13:22, Joe DeCapo <snoogansbc@gmail.com> wrote:
Since it's Playground bug, should I file it in Apple's bug reporter rather than Swift's?
On Aug 7, 2017, at 3:12 PM, Jordan Rose <jordan_rose@apple.com> wrote:
On Aug 4, 2017, at 12:41, Joe DeCapo via swift-users <swift-users@swift.org> wrote:
Hi everyone,
I'm not sure if there's a more appropriate place to ask this question, but I figured at the very least I could get pointed in the right direction. I've tried searching online and haven't been able to find anything addressing this.
I was trying to use the `defer` statement in a Playground, and was surprised to find that it never prints anything in the preview pane on the side. I was expecting the evaluation of the code in the `defer` statement to show up in line with the statements, even though they're executed after the last line in the function. I made a very simple playground that modifies a global variable and prints the value in the `defer` statement, and when I print the global variable after calling my function it shows the correct updated value, so the code in the `defer` statement is getting run as expected. Here's my sample code with the Playground output in comments on the side:
var x = 3 // 3
func doSomething() {
print(1) // "1\n"
defer {
x += 1
print(x)
}
print(2) // "2\n"
}
doSomething()
print(x) // "4\n"