Union

Forum for discussion about the documentation project.
fxm
Posts: 8962
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Union

Postby fxm » Sep 29, 2018 13:33

Yes, done.
dodicat
Posts: 5758
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Union

Postby dodicat » Sep 29, 2018 14:00

Another example;

Code: Select all

 Union switchdouble
    As Double float
    As Ulongint value
    As zstring * 60 Hex
    Declare Constructor(As Double=0)
    Declare Constructor(As String="")
    Declare Operator Let(As Double)
    Declare Operator Let(As String)
End Union

Constructor switchdouble(f As Double)
float=f
Hex=Lcase("&h"+..hex(value))
End Constructor

Constructor switchdouble(f As String)
value=Valulng(f)
End Constructor

Operator switchdouble.let(n As Double)
this.constructor(n)
End Operator

Operator switchdouble.let(n As String)
this.constructor(n)
End Operator


'===============================================================


Print
Print Sizeof(Double),"sizeof(double)"
Print Sizeof(Ulongint),"sizeof(ulongint)"
Print Sizeof( zstring * 60),"sizeof( zstring * 60) <----------------  biggest"
Print

Dim As switchdouble a,b

Do
    Dim As Double d=(rnd*1000000000-rnd*1000000000)
    a=d
    b=a.hex
    Print "random Number",,"hex(number)",,"return number"
    Print d,a.hex,b.float,Iif(d=b.float,"Looks OK","Error")
    Print
    Sleep
Loop Until Inkey=Chr(27)

 
fxm
Posts: 8962
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Union

Postby fxm » Sep 29, 2018 14:23

A little complicated for a beginner.
Many other pages to first assimilate.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Union

Postby sancho3 » Sep 30, 2018 0:33

Josep Roca wrote:A priori" is also much used in Spanish (at least in Spain).

Fantastic. Therefore this is a great line to use in the Spanish language version of the FreeBASIC documentation.

Thanks for pointing out that in my examples there are only two types in the unions.
sancho3 wrote:I see that it is not exactly breaking the rule since the nested type is treating the ubytes as one member of the union but I think the point still stands.

Granted this was an edit. It was almost immediate. It suggests that not all of what was written was read.
I suppose I should post in Spanish now? Nope, that is "nonsense".

Edit:
As to the changes to the Union page. Superb. Thanks.

I admit that I am a little miffed by the unnecessary "nonsense" crack and these are my last words on this topic (via google translate):
Si ya es un experto de todos los conocimientos, no puede interpretar las páginas de ayuda de la misma manera que lo hace la persona para la que fueron escritos.

If you are already an all knowing expert, then you cannot interpret the help pages the same way the person for whom they were written does.
Josep Roca
Posts: 415
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Union

Postby Josep Roca » Sep 30, 2018 13:30

@fxm

There is a little typo in Example 2:

Print Hex(ubgra.blue) ' Resulr: 44

Resulr instead of Result.
fxm
Posts: 8962
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Union

Postby fxm » Sep 30, 2018 13:45

Thanks, done.
Munair
Posts: 834
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Union

Postby Munair » Nov 12, 2018 6:00

sancho3 wrote:Did you mean 'priority''? Are we really saving anything by not finishing 2 letters in the spelling?

Some need to brush up on their language and do a "google" search first. In Dutch "a priori" is also used and so it is in English and other languages that borrowed from Latin based languages. At least in English, French and Dutch it means the same. Anybody can do the search himself:

https://www.merriam-webster.com/dictionary/a%20priori
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Union

Postby sancho3 » Nov 12, 2018 6:09

@Munair:
I'll take unnecessary posts for 100 Alex...
This thread is done and in the future please read the whole thread before posting.
Munair
Posts: 834
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: Union

Postby Munair » Nov 12, 2018 6:30

sancho3 wrote:@Munair:
I'll take unnecessary posts for 100 Alex...
This thread is done and in the future please read the whole thread before posting.

fxm used the term exactly as he should. and nowhere in this thread did I read an apology or statement that fxm was absolutely right. With my post I want to make clear that it is totally correct English (not just French or Spanish):
sancho3 wrote:Fantastic. Therefore this is a great line to use in the Spanish language version of the FreeBASIC documentation.

So my comment is not at all unnecessary IMO. A thread is only done when it is locked. ;-)
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Union

Postby sancho3 » Nov 12, 2018 7:23

Feel free to apologize to fxm anytime you want.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Union

Postby Tourist Trap » Dec 10, 2018 23:09

[quote="fxm"]At first:
KeyPgUnion → fxm [Added a first simple example]

edit:
see next
Last edited by Tourist Trap on Dec 11, 2018 1:14, edited 1 time in total.
badidea
Posts: 1358
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Union

Postby badidea » Dec 10, 2018 23:44

Maybe this wikipedia article helps to explain it: https://en.wikipedia.org/wiki/Union_typ ... _Structure
A simple drawing there of a 32-bit integer and a 32-bit float at the same memory location
I don't use union often, but for some uses they are awesome.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Union

Postby Tourist Trap » Dec 11, 2018 0:02

badidea wrote:Maybe this wikipedia article helps to explain it: https://en.wikipedia.org/wiki/Union_typ ... _Structure

Thank you, at least the wikipedia's article confirms that there are some difficulties.
edit : In the wiki, it is said that in the union, all the elements starts at the same address relatively to the storage, it is not said that the union occupies the same space (size)... I must miss something, no?

edit:
see next
Last edited by Tourist Trap on Dec 11, 2018 1:14, edited 1 time in total.
Tourist Trap
Posts: 2756
Joined: Jun 02, 2015 16:24

Re: Union

Postby Tourist Trap » Dec 11, 2018 0:21

From what I've read tonight, if I had to summarize this step. There is a primary concern, and a secondary point, that require more details in my opinion:
What does memory space mean? Size or address in memory?
Why not use procedure rather than function in the wording of the syntax?

I leave further remarks until the primary question is answered or I feel I won't be able to go very further until then.
Thanks again.

This below seems to be some interesting testing:

Code: Select all

UNION U
    as byte d(2)
    TYPE
        as byte i(4)
    END TYPE
END UNION

dim as U uu

uu.d(0) = 111
? uu.d(0), "union size="; sizeOf(uu), "union address="; @(uu)
uu.i(0) = 999
? "union size="; sizeOf(uu), "union address="; @(uu)
for i as integer = 0 to 2
    ? uu.i(i); ":";
next i
? :?


uu.d(0) = 11
? uu.d(0), "union size="; sizeOf(uu), "union address="; @(uu)
uu.i(0) = 999
? "union size="; sizeOf(uu), "union address="; @(uu)
for i as integer = 0 to 2
    ? uu.i(i); ":";
next i
?

sleep


The kind of result obtained:

Code: Select all

 111          union size= 5 union address=1375648
union size= 5 union address=1375648
-25: 0: 0:

 21           union size= 5 union address=1375648
union size= 5 union address=1375648
-57: 0: 0:
fxm
Posts: 8962
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Union

Postby fxm » Dec 11, 2018 6:09

UNION description update proposal:
Unions are similar to a Type structure, except that the elements of a union occupy the same a common space in memory (same memory address for all elements of the union).
Like Type, Union can use the optional Field = number specifier and supports also inheritance through the use of the Extends keyword.
Unlike Type, Union can not contain variable-length strings and arrays, and more generally fields (or can not have bases) with constructors or destructors. Therefore, Union does not support to inherit from the Object built-in type.
The size of the Union is the size of the largest data item. A data item can be an unnamed Type. Since they occupy the same a common memory space, only one element can usually be used at a given time.

Unions support member functions procedures including Constructor, Destructor, Function, Operator, Property and Sub. All members of a union are public and access control is not supported.

Nested unnamed type or union cannot have procedure members or static data members (same restriction for local named type/union).

A Union can be passed as a user defined type to overloaded operator functions.

Note: When Union extends a base, it can be confusing because no new fields are added to the base, but instead the base is added to the derived Union. This means that fields in the derived Union can share the same memory space like the base (here it doesn't matter whether the base is a Union or not). Of course it can be dangerous, but that is always the problem with Unions.
If only the base is a Union, then it won't be affected by fields from the derived UDT.
Since Union is not allowed to have complex fields (i.e. UDTs with constructor/destructor, or dynamic strings), a derived Union cannot be allowed to have (contain) a complex base.

Note:
In your above example, the nested unnamed TYPE is useless because it contains only one element.

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 2 guests