tomhrr / dale

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

Arrays cannot be function parameters, even for references, but array-pointers can #104

Closed porky11 closed 6 years ago

porky11 commented 8 years ago

Could array-references be function parameters? And why can't arrays be function parameters, structs containing arrays can (like the array concept type). should I use the array concept type instead of arrays?

tomhrr commented 8 years ago

On Fri, Sep 16, 2016 at 09:31:23AM -0700, Fabio Krapohl wrote:

Could array-references be function parameters?

Yep, that this isn't supported is just an oversight, and will be fixed.

And why can't arrays be function parameters, structs containing arrays can (like the array concept type).

I wanted to retain C's behaviour for passing arrays to functions, where the array degrades to a pointer to the first element. Given that, having array parameters that are functionally equivalent to pointers is redundant and confusing (see e.g. https://lkml.org/lkml/2015/9/3/428). This doesn't affect arrays that appear inside structs.

porky11 commented 7 years ago

Even in this case, arrays are not allowed as parameters

(def-variant vec ((vec2 ((value (array-of 2 float))))
                  (vec3 ((value (array-of 3 float))))
                  (vec4 ((value (array-of 4 float))))))
tomhrr commented 6 years ago

Array references are now supported as function parameters. With variants, though, arrays aren't allowed for a variant's parameters because those parameters are used to form the constructor. There aren't good ways to handle this: marking all constructor parameters as (ref (const ...)) would be inefficient for primitive types, and relying on PreferRefs doesn't work because the relevant concept-instantiation code has similar problems with array parameters. I think using Array concept types instead of primitive arrays is the better option here.