const as union

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

const as union

Post by badidea »

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
Moderator
Posts: 12159
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: const as union

Post by fxm »

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: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: const as union

Post by Munair »

retracted
Last edited by Munair on Jan 07, 2019 7:41, edited 1 time in total.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: const as union

Post by Munair »

retracted
Last edited by Munair on Jan 07, 2019 7:41, edited 1 time in total.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: const as union

Post by Munair »

You must include the dim statement:

Code: Select all

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

Re: const as union

Post by badidea »

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
Moderator
Posts: 12159
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: const as union

Post by fxm »

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

Re: const as union

Post by badidea »

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: 4346
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: const as union

Post by coderJeff »

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
Post Reply