Created
August 28, 2023 16:05
-
-
Save quonic/b9b92b3c061f35dbea69942dd7d49541 to your computer and use it in GitHub Desktop.
Understanding GetNewClosure. Assuming you have Pester installed, but not required. Remove Should parts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# No GetNewClosure - print $a as what ever it is before printing | |
& { | |
$a = 10 | |
$myScript = { | |
$a | |
} | |
& $myScript | |
$a = 20 | |
& $myScript | |
} | Should -Be @(10, 20) | |
# GetNewClosure - print $a as what it was at the time of creation | |
& { | |
$a = 30 | |
$myScript = { | |
$a | |
}.GetNewClosure() | |
& $myScript | |
$a = 40 | |
& $myScript | |
} | Should -Be @(30, 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment