move icon indicating copy to clipboard operation
move copied to clipboard

[Feature Request] Support type match or other type detected syntax

Open jolestar opened this issue 3 years ago • 1 comments

🚀 Feature Request

Motivation

Sometimes, We need to know the Generic Type variable's real type, and then do some logic dispatch by the type.

For example:

if(Type::type_of(token) == TokenA){

}else if(Type::type_of(token) == TokenB){

}

or by match:

match toke{ TokenA => {}, TokenB => {} }

Pitch

Describe the solution you'd like

Support match syntax.

Describe alternatives you've considered

Support some reflection function

Are you willing to open a pull request? (See CONTRIBUTING) yes

Additional context

A real use case for this feature(an ETH to STC Bridge):

https://github.com/Elements-Studio/poly-stc-contracts/blob/3ea6eb2a5c9a5572e4befe92e19d20a48ae757d0/src/modules/cross-chain/CrossChainRouter.move#L50-L61

Some related links:

  1. There was a related discussion https://github.com/diem/diem/issues/4468
  2. Starcoin's Token use a native function to get token's type https://github.com/starcoinorg/starcoin-framework/blob/88061e9584e5d34f7fb4affad6f094402ca3e2dc/sources/Token.move#L487
  3. https://github.com/starcoinorg/starcoin/issues/3178

jolestar avatar Mar 21 '22 08:03 jolestar

This in the form as suggested requires a native/builtin support for type reflection, which will never be supported. I understand that it is possible to reflect on the type of a value with the global storage system, but this is not something I think we really want to encourage.

In the future, we will likely add enums/variants, which will likely end up being similar to Rust style enums. Hopefully that will help in this usecase :)

tnowacki avatar Mar 21 '22 20:03 tnowacki

Close this, this feature will support by https://github.com/move-language/move/pull/566

jolestar avatar Oct 11 '22 02:10 jolestar