Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Last active December 16, 2017 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Papierkorb/43647d6c8d37274de45296df7ebec0c8 to your computer and use it in GitHub Desktop.
Save Papierkorb/43647d6c8d37274de45296df7ebec0c8 to your computer and use it in GitHub Desktop.
Demo on why `case x = @x` is necessary
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