[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Initializing structs with mixed non-empty fields and {} leads to emp
From: |
Ben Abbott |
Subject: |
Re: Initializing structs with mixed non-empty fields and {} leads to empty structs |
Date: |
Sun, 25 Mar 2012 20:14:03 -0400 |
On Mar 25, 2012, at 7:17 PM, Richard Crozier wrote:
> On 25/03/2012 22:55, PhilipNienhuis wrote:
>
>> I'm not sure if this is a bug, so I ask here first:
>>
>> ## Case 1
>> octave-3.6.1.exe:1> s = struct ("a", {}, "b", 0)
>> s =
>> 0x0 struct array containing the fields:
>> a
>> b
>> octave-3.6.1.exe:2> s.b = 65
>> error: invalid assignment to cs-list outside multiple assignment
>>
>> ## Case 2
>> octave-3.6.1.exe:4> s = struct ("a", [], "b", 0)
>> s =
>> scalar structure containing the fields:
>> a = [](0x0)
>> b = 0
>> octave-3.6.1.exe:5> s.b = 65
>> s =
>> scalar structure containing the fields:
>> a = [](0x0)
>> b = 65
>> octave-3.6.1.exe:6> s.a = {}
>> s =
>> scalar structure containing the fields:
>> a = {}(0x0)
>> b = 65
>>
>> In case 1, assigning {} to a new struct doesn't work, while it can be
>> assigned once the struct exists (see Case 2). That looks inconsistent to me.
>> s(1).b = 65 succeeds, so apparently the initial size (dimension) of the
>> struct was set to 0x0 even while a non-empty field was specified in the
>> constructing statement.
>>
>> Philip
>
> I have coincidentally come up against this bizarre behaviour in ML
> today, it seems ML creates an array of structures for cell array input
> to the struct function (rather than just putting the cell array in a
> field). Perhaps Octave also works like this for compatibility.
>
> e.g. in ML
>
>>> s = struct('a', [1 2 3 4 4 5 5], 'b', 'jfdkafjda', 'c', {1, 2, 'fda'})
>
> s =
>
> 1x3 struct array with fields:
> a
> b
> c
>
> each struct member has the same a nd b fields and one of each of the c
> cell array members. This was not intuitive to me.
>
> Your code in ML gives:
>
>>> s = struct ('a', {}, 'b', 0)
>
> s =
>
> 0x0 struct array with fields:
> a
> b
>
>>> s.b = 65
> ??? A dot name structure assignment is illegal when the structure is empty.
> Use a subscript on the structure.
>
>
> Richard
This seems to be a recurring problem. I've entered a feature request in the
hope of more informative error message can be given.
https://savannah.gnu.org/bugs/index.php
Ben