Created
October 23, 2019 19:16
-
-
Save lupuszr/702b911ae58d471ab484ece3f6053018 to your computer and use it in GitHub Desktop.
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
abstract class Season { | |
cata<W, Su, Sp, Au>(match: { | |
Winter: () => W, | |
Summer: () => Su, | |
Autumn: () => Au, | |
Spring: () => Sp | |
}) { | |
type constraintsT = keyof typeof match; | |
switch (this.constructor.name as unknown as constraintsT) { | |
case 'Winter': { | |
return match['Winter']() | |
} | |
case 'Summer': { | |
return match['Summer'](); | |
} | |
case 'Autumn': { | |
return match['Autumn'](); | |
} | |
case 'Spring': { | |
return match['Spring'](); | |
} | |
} | |
} | |
} | |
class Summer extends Season { constructor() { super() } } | |
class Autumn extends Season { constructor() { super() } } | |
class Winter extends Season { constructor() { super() } } | |
class Spring extends Season { constructor() { super() } } | |
const la = new Summer(); | |
const next = la.cata({ | |
Winter: () => new Spring(), | |
Summer: () => new Autumn(), | |
Autumn: () => new Winter(), | |
Spring: () => new Summer(), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment