|
|
|
|
|
by jesseschalken
3741 days ago
|
|
ADTs can be very easily replaced with visitors. type Maybe<T> = <V>(v:{just(x:T):V, nothing():V}) => V
let just = <T>(x:T):Maybe<T> => <V>(v:{just(x:T):V}) => v.just(x)
let nothing = <T>():Maybe<T> => <V>(v:{nothing():V}) => v.nothing()
let values:Maybe<number>[] = [just(6), nothing()]
values.forEach(maybe => {
alert(maybe({
just: x => 'got: ' + x,
nothing: () => 'nothing'
}))
})
|
|