mlhaufe / brevity

Brevity is a library that enables Feature-Oriented Programming (FOP) and solves the expression problem in a manner that makes data and operation declarations trivial to define and compose.
GNU Affero General Public License v3.0
1 stars 0 forks source link

Allow primitives in Trait declarations #35

Closed mlhaufe closed 1 year ago

mlhaufe commented 1 year ago

Current approach on fibonacci and similar function require an undefined dataDecl and the use of [all]:

const fib = Trait(undefined, {
  [all]: (n) => n == 0 ? 0 :
                n == 1 ? 1 :
                fib(n-1) + fib(n-2)
})

Allowing primitives and resolving #34 would enable the following:

const fib = Trait(Number, {
  0: () => 0,
  1: () => 1,
  _: (n) => fib(n-1) + fib(n-2)
})

Primitives to support:

Trait(Number, {
  1: () => {},
  [Infinity]: () => {},
  [Number.EPSILON]: () => {},
  [Number.MAX_SAFE_INTEGER]: () => {},
  [Number.MAX_VALUE]: () => {},
  [Number.MIN_VALUE]: () => {},
  [Number.NaN]: () => {},
  [NaN]: () => {},
  [Number.POSITIVE_INFINITY]: () => {},
  [Number.NEGATIVE_INFINITY]: () => {},
})

Trait(Boolean, {
  false: () => {},
  true: () => {}
})

Trait(BigInt, {
   [2n]: () => {}
})

Trait(String, {
  'arbitrary string': () => {}
})