capi icon indicating copy to clipboard operation
capi copied to clipboard

consistent approach to `PatternRune.from`

Open harrysolovay opened this issue 1 year ago • 0 comments

Ideally, all PatternRunes would have a static from method, which could be called with the given chain, a runic arg value/T and parent (or omitted if P is void).

Pattern implementors could then decide whether or not to attach other static methods (which we could provide) for even simpler instantiation. One such example is a fromRec, with which an object-subtyped T could be specified as runic fields, instead of as a single rune.

export class MultisigRune<out C extends Chain, out U> extends PatternRune<Multisig, C, U> {
  // ...

  static fromRec = patternFromRec
}

Some other pattern-specific factories:

  • fromText
  • fromBytes
  • fromHex
  • fromCodec
  • fromNft

For posterity, here's a failed attempt to create this static from method on PatternRune. While type errors could be overcome, the return subclass signature could not (at least in my initial attempts).

static from<
  T,
  C extends Chain,
  U,
  P,
  This extends {
    _prime: (batch: Batch) => Run<T, U>
    new(
      _prime: This["_prime"],
      chain: ChainRune<C, U>,
      parent: P,
    ): PatternRune<T, C, U, P>
  },
  X,
>(
  this: This,
  chain: ChainRune<C, U>,
  value: RunicArgs<X, [T]>[0],
  parent: P,
) {
  return Rune.resolve(value).into(this, chain, parent)
}

harrysolovay avatar Mar 31 '23 11:03 harrysolovay