Skip to content

Instantly share code, notes, and snippets.

@lupuszr
Created October 23, 2019 19:16

Revisions

  1. lupuszr revised this gist Oct 23, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion DataConstructors.ts
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,6 @@ abstract class Season {
    type constraintsT = keyof typeof match;
    switch (this.constructor.name as unknown as constraintsT) {
    case 'Winter': {
    const t = this as unknown as Winter;
    return match['Winter']()
    }
    case 'Summer': {
  2. lupuszr created this gist Oct 23, 2019.
    39 changes: 39 additions & 0 deletions DataConstructors.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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': {
    const t = this as unknown as 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(),
    })