Skip to content

Instantly share code, notes, and snippets.

@fukaoi
Last active June 21, 2018 11:24
Show Gist options
  • Select an option

  • Save fukaoi/6a255fad2a9273ec2e2d2c81818adeaa to your computer and use it in GitHub Desktop.

Select an option

Save fukaoi/6a255fad2a9273ec2e2d2c81818adeaa to your computer and use it in GitHub Desktop.
Crystal Monomorphism example code
abstract class Parent #同じシグネチャなので、abstractを通して、制約をつける
abstract def disp
end
class A < Parent
def disp : String
"call A"
end
end
class B < Parent
def disp : String
"call B"
end
end
module Switcher
def self.new(type : Symbol) : Parent
if type == :A
A.new
elsif type == :B
B.new
else
raise "Not found symbol"
end
end
end
puts Switcher.new(:A).disp
puts Switcher.new(:B).disp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment