[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [RFC] Syntax for struct type constructors
From: |
Jose E. Marchesi |
Subject: |
Re: [RFC] Syntax for struct type constructors |
Date: |
Thu, 04 Mar 2021 20:03:33 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
>> On Mon, Mar 01, 2021 at 10:03:55PM +0100, Jose E. Marchesi wrote:
>>>
>>> Hi people!
>>>
>>> Next feature I will implement in poke is support for having arguments in
>>> struct types. Like this:
>>>
>>> type Packet =
>>> struct (int control, int datasize = 256)
>>> {
>>> byte ctrl = control;
>>> byte[datasize] payload;
>>> };
>>>
>>> I feel good with the above syntax: it in intuitive and clear.
>>>
>>> [...]
>>>
>>> 2) To use angle brackets, like in:
>>>
>>> Packet<2, 1024>
>>> Packet<2> /* datasize is optional */
>>>
>>
>> Is this valid?
>>
>> ```poke
>> var ctl = 100;
>> var pkt = Packet<ctl>{};
>>
>> ctl = 200;
>>
>> assert (pkt.ctrl == 100); // I think this is the case, because `ctl` will be
>> copied,
>> // but the story will be different for composite
>> types
>> // or,
>> // assert (pkt.ctrl == 200); // ?!
>> ```
>
> Passing arguments to the type constructors works exactly the same than
> passing arguments to functions.
>
> So yes, if the value is composite and you modify it, it will be visible
> inside the struct type.
>
> This is similar to what we have now in type arrays:
>
> (poke) var N = 2
> (poke) type Matrix = int[N][N]
> (poke) Matrix ()
> [[0x0,0x0],[0x0,0x0]]
> (poke) N = 3
> (poke) Matrix ()
> [[0x0,0x0,0x0],[0x0,0x0,0x0],[0x0,0x0,0x0]]
Ok, I realize I am contradicting myself :)
The arguments in type specifiers like int[N][N] do _not_ work like
passing arguments to functions. Thats the reason why even if N is a
simple type, it is used like the expression would be reevaluated in the
same context.
On the other hand, arguments to struct types like in:
Packet<ctl> { }
would be passed as actuals. In that case, passing a composite value and
changing it later may impact the Packet mapper, constructor, etc.