Skip to Content
2
Feb 14, 2022 at 10:36 AM

Using enums not just as types but also as references for constants

274 Views Last edit Feb 14, 2022 at 10:38 AM 2 rev

Dear CAP team,

I would like to come with an additional proposal for this amazing framework.

So we have enums right now:

type Rating : Integer enum {
  Best  = 5;
  Good  = 4;
  Avg   = 3;
  Poor  = 2;
  Worst = 1;
}

which we can use as a type and it's already great.

entity Reviews {
  key ID   : UUID;
  rating   : Rating;
}<br>

However what could be really cool if we could use also those values as constants in our model.

Just for example

view BestReviews as select from Reviews where rating = Rating.Best

or even like that

entity as select from SapEvents {
  Events.SAP as source,
...
} union all select from NonSapEvents {
  Events.NON_SAP as source
}


type Events : Integer enum {
  SAP = 1;
  NON_SAP = 2;
} 

This way we can avoid of hardcoded values in our models in constant fields, where and joins conditions and navigation paths. It will help to refactor code also quickly and will help to search for references in the code faster.

What do you think?

Thanks!