Last active
June 21, 2018 11:24
-
-
Save fukaoi/6a255fad2a9273ec2e2d2c81818adeaa to your computer and use it in GitHub Desktop.
Crystal Monomorphism example code
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
| 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