Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct prefix{
- enum tag{
- t1,
- t2
- } t;
- int some_shared_data;
- int more_shared_data;
- }
- struct t1{
- prefix p;
- int t1specific;
- @proxy p;
- }
- struct t2{
- prefix p;
- string t2specific;
- @proxy p;
- }
- @patternmatching(prefix){
- function t1pattern(prefix* p, t1** out): bool{
- if ( p->t == prefix::tag::t1 ){
- *out = (t1*)p;
- return true;
- }
- return false;
- }
- function t2pattern(prefix* p, t2** out): bool{
- if ( p->t == prefix::tag::t2 ){
- *out = (t2*)p;
- return true;
- }
- return false;
- }
- }
- void use(prefix* p){
- switch ( p ){
- case t1pattern(concrete):
- // concrete is of type t1*
- case t2pattern(concrete):
- // concrete is of type t2*
- // enforces that you covered all the cases (or requires a default one)
- }
- // unfolds into
- {
- t1* concrete;
- if ( t1pattern(p, &concrete) ){
- // concrete is of type t1*
- }else{
- t2* concrete;
- if ( t2pattern(p, &concrete) ){
- // concrete is of type t2*
- }else{
- unreachable!();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement