Last active
December 16, 2017 17:14
-
-
Save Papierkorb/43647d6c8d37274de45296df7ebec0c8 to your computer and use it in GitHub Desktop.
Demo on why `case x = @x` is necessary
This file contains hidden or 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
class Foo | |
@stuff : String | Int32 | |
@waiter = Channel(Nil).new | |
def initialize(@stuff) | |
spawn do | |
@waiter.receive # Simulate waiting for some IO | |
@stuff = "Oh noes!" # And store the result of it | |
end | |
end | |
def do_stuff | |
case @stuff | |
when String | |
puts "Don't mind me." | |
when Int32 | |
puts "@stuff is a #{@stuff.class}" # <- @stuff is a Int32, everything is great | |
@waiter.send(nil) # <- Someone calls another fiber | |
puts "@stuff is a #{@stuff.class}" # <- @stuff is still a Int32 .. or is it? | |
end | |
end | |
end | |
Foo.new(123).do_stuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment