const as union

General FreeBASIC programming questions.
badidea
Posts: 1177
Joined: May 24, 2007 22:10
Location: The Netherlands

const as union

Postby badidea » Jan 06, 2019 23:41

I was trying some crazy stuff. A union constant. It is possible, but only in a certain way is seems. See code:

Code: Select all

dim as integer X1 = 2
const as integer X2 = 2
dim as const integer X3 = 3

type abType
   dim as short a, b
end type

dim as abType AB1 = type(1, 1)
const as abType AB2 = type(1, 1) '<-- Not allowed
dim as const abType AB3 = type(1, 1)

union baUnion
   dim as long ba
   type
      dim as short b,a
   end type
end union

dim as baUnion BAU1
const as abUnion ABU2 '<-- Not allowed
dim as const baUnion BAU3 = 1 '<-- Not ok, how to set?

union abUnion
   type
      dim as short a, b
   end type
   dim as long ab
end union

dim as abUnion ABU1
dim as const abUnion ABU3 = type(1, 1) '<-- OK

print ABU3.ab
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: const as union

Postby fxm » Jan 07, 2019 6:21

Code: Select all

dim as integer X1 = 2
const as integer X2 = 2
dim as const integer X3 = 3

type abType
   dim as short a, b
end type

dim as abType AB1 = type(1, 1)
const as abType AB2 = type(1, 1) '<-- Not allowed
dim as const abType AB3 = type(1, 1)

union baUnion
   dim as long ba
   type
      dim as short b,a
   end type
end union

dim as baUnion BAU1
const as baUnion ABU2 = type(1) '<-- Not allowed
dim as const baUnion BAU3 = type(1) '<-- OK

union abUnion
   type
      dim as short a, b
   end type
   dim as long ab
end union

dim as abUnion ABU1
dim as const abUnion ABU3 = type(1) '<-- OK
dim as const abUnion ABU4 = type(1, 1) '<-- OK

print ABU3.ab
Munair
Posts: 828
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: const as union

Postby Munair » Jan 07, 2019 7:32

retracted
Last edited by Munair on Jan 07, 2019 7:41, edited 1 time in total.
Munair
Posts: 828
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: const as union

Postby Munair » Jan 07, 2019 7:37

retracted
Last edited by Munair on Jan 07, 2019 7:41, edited 1 time in total.
Munair
Posts: 828
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: const as union

Postby Munair » Jan 07, 2019 7:40

You must include the dim statement:

Code: Select all

dim as const abType AB2 = type(1, 1) '<-- allowed
badidea
Posts: 1177
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: const as union

Postby badidea » Jan 07, 2019 19:48

Thanks, my conclusion:
A) const as ... is not possible, use dim as const ...
B) The order of variables within the union is important for assigning a value

But then I got confused again:

Code: Select all

union abUnion
   type
      dim as short a, b
   end type
   dim as long ab
end union

dim as const abUnion ABU1 = type(&hffffffffl) '<-- OK
print ABU1.a, ABU1.b

dim as const abUnion ABU2 = type(&hffffffffl, &hffffffffl) '<-- OK ?
print ABU2.a, ABU2.b

dim as const abUnion ABU3 = type(&hffffffffl, &hfffffffl) '<-- NOK ?
print ABU3.a, ABU3.b
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: const as union

Postby fxm » Jan 07, 2019 20:52

Short => 16-bit signed (or 4 hexadecimal digits).
badidea
Posts: 1177
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: const as union

Postby badidea » Jan 07, 2019 22:28

fxm wrote:Short => 16-bit signed (or 4 hexadecimal digits).

I know, but why is &hffffffffl (8 x f) ok and &hfffffffl (7 x f) wrong?

I also noticed that 'const as ...' is global and 'dim as const ...' is not, so it becomes 'dim shared as const vartype var = type(value)' (lots of text).
coderJeff
Site Admin
Posts: 2794
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: const as union

Postby coderJeff » Jan 08, 2019 22:24

CONST as described in CONST wiki page, even though it has a data type, is handled like a literal. Only the compiler knows about it's type. No memory is allocated for it and, can't take the address of it.

Code: Select all

#define x0 cint(0)
const as integer x1 = 1
dim as integer x2 = 2

print @x0 '' error
print @x1 '' error
print @x2 '' OK


CONST as described in CONST (Qualifier) wiki page, tells the compiler that the variable, even though it has an address, can not be modified.

Code: Select all

dim as const integer x3 = 3
print @x3  '' OK - it has an address
x3 = 33    '' error, can't modify

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests