ferrum icon indicating copy to clipboard operation
ferrum copied to clipboard

Custom struct infrastructure

Open koraa opened this issue 4 years ago • 1 comments

Js structs have a number of disadvantages:

  • methods are unbound
  • new construction
  • This behaves weirdly.
  • No explicit handling of

We could substitute our own syntax:

// Foo extends Bar
const Foo = struct('Foo', Bar, {
  // property; must be set during initialization
  'foo': undefined,
  // property with default value (supplied using deep clone)
  'foo': 42,
  // property, with value generated by calling the function
  'foo': Prop.init((self) => undefined),
  // property; with getter/setter
  'foo': Prop({
    get: (self) =>,
    set: (self) =>,
  }),
  // property; 
  // property, assigned by calling the provided function
  // Bound method syntax
  'foo': (self) => …
  // Static bound method/property/etc
  '@foo': (cls) => …
});

Only hard rule is: @ is for static, no @ is instance. Provide some trait intoProp or something that contains normalization rules for properties that turn everything into a Property({ init(), get(), set() }). The normalization rule for undefined is to throw (to enforce explicit initialization). The normalization rule for Functions is binding on initialization and hiding from enumerable properties and so on.

By default .init(foo, bar, baz) and .namedInit({ foo, bar, baz }) is provided.

Highly flexible; metaprogrammable; supports reflection.

koraa avatar Apr 15 '21 15:04 koraa

Also provide tuple as a special case of/wrapper around struct. Tuple works like struct but assumes property declarations are in order (cannot use object literal) and implements object destructuring.

koraa avatar Apr 30 '21 18:04 koraa