1

I have a struct with a list:

struct my_struct {
    my_list : list of uint;
    // other fields
};

I would like to build something similar to cpp class constructor: when I allocate my_struct using new operator, my_list will be initiated to have one zero element - my_list == { 0 };

Is there a way to build a struct constructor in e?

Thank you for your help

Halona
  • 1,475
  • 1
  • 15
  • 26

2 Answers2

1

yes, any_struct has the init() method. it is called when the struct is created with new(), and also when the struct is generated (before the generation, the constraints solving, starts).

if you want this list to always have same init value, regardless if created with new or with gen, mark this list field as do-not-generate.

struct my_struct {
   !l : list of_ int;
   init() is also {
      l = {0};
   };
}; 
user3467290
  • 706
  • 3
  • 4
1

In e there is no "general" concept of parameterized constructors like in C++, but as mentioned in the other answer, there is a predefined init() method (without parameters) which gets automatically called whenever a new object of a given type is created. It is important to notice that init() is called whenever an object of that type is created, no matter in what way its creation happens: it may be an explicit creation via new, an implicit creation as part of pre-run generation, and so on.

Another important point: if you just need to assign a specific constant value to a field, you can also specify this value directly at the field declaration, not necessary to do it via init(), for example:

struct my_struct {
    my_list : list of uint = {0};  // field declared with an initializer
};
Yuri Tsoglin
  • 963
  • 4
  • 7