Having extensive experience as a javascript developer, I recently delved into learning C# as my first statically typed language. My upcoming project involves using TypeScript, so I've been refreshing my knowledge on it.
Below is the code I have written:
interface IMonad<T> {
get(): T;
set<T>(fn: (value: T) => T): IMonad<T>;
}
class LazyMonad<T> implements IMonad<T>
{
private value: T;
private binds;
constructor(value: T)
{
this.value = value;
this.binds = [];
}
get(): T
{
return this.binds
.reduce(function (v: T, fn): T {
return (v === null) ? null : v + fn(v);
}, this.value);
}
set<T>(fn: (value: T) => T): LazyMonad<T>
{
this.binds.push(fn);
return this;
}
}
Edit: Additionally, here is another class that implements IMonad<T>
:
class IdentityMonad<T> implements IMonad<T>
{
private value: T;
constructor(value: T)
{
this.value = value;
}
get(): T
{
return this.value;
}
set<T>(fn: (value: T) => T): IdentityMonad<T>
{
return new IdentityMonad<T>(fn(this.value));
}
}
I encountered this error while running tsc
:
src/lazy_monad.ts(25,10): error TS2322: Type 'this' is not assignable to type 'LazyMonad'. Type 'LazyMonad' is not assignable to type 'LazyMonad'. Type 'T' is not assignable to type 'T'.
I suspect there might be an issue with my implementation. Would appreciate any advice, especially if you have experience with both TypeScript and C#. Thanks!