Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Types #3

Open
lsjroberts opened this issue Nov 29, 2018 · 4 comments
Open

Custom Types #3

lsjroberts opened this issue Nov 29, 2018 · 4 comments
Labels
discussion Ideas open for discussion

Comments

@lsjroberts
Copy link
Contributor

Custom types are also known as "union types", "tagged unions" and "algebraic data types".

While typescript provides a way to have compile-time union types, they are no longer available at runtime, which means they can't be used for active behaviours (such as messages in wool/browser).

Basic usage

import { Type } from 'wool/core';

const Increment = Type.custom('Increment');
const Decrement = Type.custom('Decrement');

let value = 0;

const msg = Increment();

Type.matchOn(msg, {
  [Increment]() { value = value + 1; },
  [Decrement]() { value = value - 1; },
});

console.log(value);
// 1

Parameters

import { Type } from 'wool/core';

const SetValue = Type.custom('SetValue', Type.int);

const msg = SetValue(42);

Type.matchOn(msg, {
  // ...
  [SetValue](newValue) { value = newValue; },
});

console.log(value);
// 42
import { String, Type } from 'wool/core';

const SetGreeting = Type.custom('SetGreeting', Type.string, Type.int)

let value = 'Hi';

const msg = SetGreeting('Hello', 3);

Type.matchOn(msg, {
  [SetGreeting](greeting, times) {
    value = String.repeat(greeting, times);
  },
});

console.log(value);
// "HelloHelloHello"
@lsjroberts lsjroberts added the discussion Ideas open for discussion label Nov 29, 2018
@lsjroberts
Copy link
Contributor Author

lsjroberts commented Nov 30, 2018

A better alternative:

const Msg = Type.custom({
  Increment: [],
  SetValue: [Type.int],
  SetGreeting: [Type.string, Type.int],
});

const msg = Msg.Increment();
// Msg.SetValue(42);
// Msg.SetGreeting('Hello', 3);

Msg.match(msg, {
  Increment() {
    value = value + 1;
  },
  SetValue(newValue) {
    value = newValue;
  },
  SetGreeting(greeting, times) {
    value = String.repeat(greeting, times);
  },
});

Msg.match would throw an error if not all the names were accounted for.

@lsjroberts
Copy link
Contributor Author

lsjroberts commented Nov 30, 2018

Though it should be feasible to have a default match as well:

Msg.match(msg, {
  Increment() {
    value = value + 1;
  },
  _() {
    // do some default action
  },
});

@lsjroberts
Copy link
Contributor Author

And it should return the returned value:

value = Msg.match({
  Increment: () => value + 1,
  // ...
})

@lsjroberts
Copy link
Contributor Author

Is Msg.case a better name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Ideas open for discussion
Projects
None yet
Development

No branches or pull requests

1 participant