tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.03k stars 48 forks source link

Initializing struct with incomplete parameter list #124

Closed porky11 closed 7 years ago

porky11 commented 7 years ago

I have a struct and most values should be zero. I dont want to set all fields to zero when creating a new one. I could define init, that sets all values to zero, but this seems like unneccessary overhead, especially in cases, where many fields are defined when declaring the struct. And I don't think that init or something similar is called, when using an initializer list, but some inbuild macroexpansion, which creates a struct (alloca) and sets them all to the values that are defined. So there is no easy way, to set only the undeclared values to zero (or any other value). Being able to declare default-values for structs seems like a good solution, so that I'd be able to write something like this:

(def x (struct intern
  ((a int) ;no default vlaue
   (b int 0) ;default value zero
   (c int (+ some-var 1))))) ;default values may also contain code

I'm not sure, if this should be implemented as a macro or built in

tomhrr commented 7 years ago

I haven't looked at #139 in detail yet, but if this were to be done, it would happen along those lines. Allowing literals to be included in a struct definition raises fun problems, like defining when the forms would be executed, how they'd be copied from one place to another, whether they could refer to earlier struct values (and providing access to those values), and possibly others. Even if there are sensible solutions for each of these, it's extra complication in the core, when allowing macros to be used for initialisation seems like a much less complicated alternative.