ecmascript-types icon indicating copy to clipboard operation
ecmascript-types copied to clipboard

Theorize how reflection would work with and without decorators

Open sirisian opened this issue 3 years ago • 0 comments

WIP. This isn't important to the proposal really. Just a place for me to jot down notes.

Server contract example.

@contract('contract')
class Contract {
  add(a:number, b:number):number {
    return a + b;
  }
  
  @oneWay
  updateProgress(
    @validate(v => v >=0 && v <= 1)
    progress:number
  ):void {
    /* do stuff */
  }
  
  simple(
    @validate(v => v.startsWith('a'))
    s:string
  ):string {
    return s + t;
  }

  @validate((a, b, c) => b + c < 10, 'B + C must be less than 10') // Returned error. Others are silent.
  complex(
    { 
      (a:number),
      @validate(v => v <= 10)
      (b:uint8) = 0
    }, [
      @validateRange(1, 10)
      c:uint8
    ]
  ):MyInterface {
    return foo(a, b, c);
  }
  
  foo(
    @validate(a:number => a < 5)
    @validate(a:string => a =='a')
    a:number|string = 'a'
  ):@validate(s => s.length != 0) string { // return validation
      return a.toString();
  }
}

TODO: Add actually complex cases with union types and parameter decorators. Use interfaces and such to really iron out edge cases.

const client = await RPCClient('contract');
const a = await client.add(1, 2);
client.updateProgress(0.5);
const s = await client.simple('abc');
const b = await client.complex({ a: 1.5 }, [10]);

Decorators would definitely need to be able to extract types.

TODO: Define decorators for above cases.

sirisian avatar Feb 11 '22 07:02 sirisian