Union types?

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
8 posts • Page 1 of 1
Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Union types?

Post by Smiffy »

Does CONTROL support union data types?

leoknipp
Posts:2928
Joined: Tue Aug 24, 2010 7:28 pm

Re: Union types?

Post by leoknipp »

Probably you can use the CTRL datatype "anytype" to fulfill the requirements.

Best Regards
Leopold Knipp
Senior Support Specialist

Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Re: Union types?

Post by Smiffy »

Don't think so. Union types are overlaid structures. They both exist at the same address.
For example the code:-
union conv
{
float fVal;
byte bVal[4]
} u1;

Declares a float and 4 bytes at the same address. So you can store a float or 4 bytes into the same address, and access either via the u1 variable.

adaneau
Posts:310
Joined: Tue Feb 21, 2012 9:49 am

Re: Union types?

Post by adaneau »

Hi,

Couldnt this be done using struct? Union and struct (besides memory use) are basically the same thing.

BR
Alexandre

Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Re: Union types?

Post by Smiffy »

Indeed it can. I have done it now. I have used a dyn_anytype and mixed different structures in it.
However, as per normal with programming, when one door open another slams in your face.
I have a list of structures now stored in a dyn_anytype and would like to sort them alphabetically by a string member which is in both structure types.
Structure A has a string "name" as its first member. Stricture B also has a string "name" as its first member.
I want to sort the structure list (dyn_anytype) by the name field.
dynSort and dynDynSort seem the likely candidates, but they seem unhappy to sort by string types.

adaneau
Posts:310
Joined: Tue Feb 21, 2012 9:49 am

Re: Union types?

Post by adaneau »

Hi,
Maybe with a mapping instead of dyn_anytype? So you can use the string as key and sort it

BR
Alexandre

Smiffy
Posts:45
Joined: Thu May 18, 2017 4:21 pm

Re: Union types?

Post by Smiffy »

Sorted it now. Sort will work with string types. However, it won't work with structure members. The solution was to have a dyn_dyn_anytype. The lower "array" would contain string (the name) at index 1, then Structure A at index 2, and Structure B at index 3 (constituting the "entry"). This effectively makes indexes 2 and 3 a union type definition. This dynmaic any type (individual "entry") can then nested in another dynamic any type for the amount of entries required. Then the function dynDynSort(EntriesList, 1); can be used to sort the entries by the name (string).

gschijndel
Posts:376
Joined: Tue Jan 15, 2019 3:12 pm

Re: Union types?

Post by gschijndel »

If you really want to store the value only once and read different types, you could store the value as a blob and read the value back into the wanted type.
You could encapsulate this in a class and create functions to get the value back as the different types.

8 posts • Page 1 of 1