[Feature Request] Support type match or other type detected syntax
🚀 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:
- There was a related discussion https://github.com/diem/diem/issues/4468
- Starcoin's Token use a native function to get token's type https://github.com/starcoinorg/starcoin-framework/blob/88061e9584e5d34f7fb4affad6f094402ca3e2dc/sources/Token.move#L487
- https://github.com/starcoinorg/starcoin/issues/3178
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 :)
Close this, this feature will support by https://github.com/move-language/move/pull/566