hyperledger / iroha-javascript

JavaScript library for Iroha, a Distributed Ledger Technology (blockchain) platform.
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
94 stars 64 forks source link

Feature proposal: evaluate `Expression` on type-level #144

Closed 0x009922 closed 5 months ago

0x009922 commented 1 year ago

Expression is a data-model structure that allows to build simple or complex expressions that are evaluated to something at runtime in Iroha.

TODO: show expression examples

Currently Expression implementation in TypeScript is completely dynamic and cannot guarantee that built Expression will not fail to evaluate at runtime. However, I think that evaluation mechanism is pretty determined, and Expression can be described as type-strong discriminated unions, thus, it is possible to develop a type that will compute the resulting type of an expression based on its shape.

I think this feature requires, first of all, improvement of @scale-codec/definition-compiler so it will generate enum factories that builds a concrete enum variant instead of telling TypeScript that build value is any of variants:

// now:

const val1 = OptionStr('Some', '4')
// type: OptionStr

// should be:

const val2 = OptionStr('Some', '4')
// type (roughly): { tag: 'Some', content: '4' } 

This change will allow to give TypeScript precise information about the built enums.

Secondly, iroha-javascript should provide:

export type EvaluateExpression<E extends Expression> = ... // evaluation prediction

Finally, there should be convenient builders that leverate this util type and will allow to build instructions with checked expressions, which should give a huve DX improvement.

Related issue in Java SDK:

0x009922 commented 10 months ago

Related issue:

mversic commented 5 months ago

we don't have expressions in the core anymore