Skip to content

Instantly share code, notes, and snippets.

@susisu
Last active April 29, 2019 02:43
Show Gist options
  • Save susisu/6b0a2a8ccccb28c8f6908e3d9df3dc40 to your computer and use it in GitHub Desktop.
Save susisu/6b0a2a8ccccb28c8f6908e3d9df3dc40 to your computer and use it in GitHub Desktop.
const ref = <T>(f: (x: T) => T) => {
const r = { val: f };
return () => r;
};
const id = <T>(x: T) => x;
// r: <T>() => { val: (x: T) => T }
const r = ref(id);
// unsafe instantiations of r
const r1 = r<number>();
const r2 = r<string>();
// update r1.val with a function specific to numbers
r1.val = (x: number) => x * 2;
// using r2.val will cause runtime failure
r2.val("foo");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment