Skip to content

Instantly share code, notes, and snippets.

@lupuszr
Created October 23, 2019 19:16
Show Gist options
  • Save lupuszr/702b911ae58d471ab484ece3f6053018 to your computer and use it in GitHub Desktop.
Save lupuszr/702b911ae58d471ab484ece3f6053018 to your computer and use it in GitHub Desktop.
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