Union
Re: Union
Yes, done.
Re: Union
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)
Re: Union
A little complicated for a beginner.
Many other pages to first assimilate.
Many other pages to first assimilate.
Re: Union
Fantastic. Therefore this is a great line to use in the Spanish language version of the FreeBASIC documentation.Josep Roca wrote:A priori" is also much used in Spanish (at least in Spain).
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.
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: Union
@fxm
There is a little typo in Example 2:
Print Hex(ubgra.blue) ' Resulr: 44
Resulr instead of Result.
There is a little typo in Example 2:
Print Hex(ubgra.blue) ' Resulr: 44
Resulr instead of Result.
Re: Union
Thanks, done.
Re: Union
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:sancho3 wrote:Did you mean 'priority''? Are we really saving anything by not finishing 2 letters in the spelling?
https://www.merriam-webster.com/dictionary/a%20priori
Re: Union
@Munair:
I'll take unnecessary posts for 100 Alex...
This thread is done and in the future please read the whole thread before posting.
I'll take unnecessary posts for 100 Alex...
This thread is done and in the future please read the whole thread before posting.
Re: Union
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:@Munair:
I'll take unnecessary posts for 100 Alex...
This thread is done and in the future please read the whole thread before posting.
So my comment is not at all unnecessary IMO. A thread is only done when it is locked. ;-)sancho3 wrote:Fantastic. Therefore this is a great line to use in the Spanish language version of the FreeBASIC documentation.
Re: Union
Feel free to apologize to fxm anytime you want.
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Union
Last edited by Tourist Trap on Dec 11, 2018 1:14, edited 1 time in total.
Re: Union
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.
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.
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Union
Thank you, at least the wikipedia's article confirms that there are some difficulties.badidea wrote:Maybe this wikipedia article helps to explain it: https://en.wikipedia.org/wiki/Union_typ ... _Structure
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.
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Union
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:
The kind of result obtained:
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
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:
Re: Union
UNION description update proposal:
In your above example, the nested unnamed TYPE is useless because it contains only one element.
Note:Unions are similar to a Type structure, except that the elements of a union occupythe samea 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 occupythe samea common memory space, only one element can usually be used at a given time.
Unions support memberfunctionsprocedures 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.
In your above example, the nested unnamed TYPE is useless because it contains only one element.