UDT initializer : Invalid data types

General FreeBASIC programming questions.
Post Reply
Landeel
Posts: 777
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

UDT initializer : Invalid data types

Post by Landeel »

Code: Select all

union colortype
        bgra as uinteger<32>
        type
                b as ubyte
                g as ubyte
                r as ubyte
                a as ubyte
        end type
end union

type mytype
        clr as colortype=rgba(255,255,255,255)
end type
This code is obviously wrong, but it used to compile and work.

With current git fbc, it gives me a:
c.bas(12) error 24: Invalid data types in 'clr as colortype=rgba(255,255,255,255)'
How do I write this correclty? Can I use 'cast' in the initializer? How?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: UDT initializer : Invalid data types

Post by D.J.Peters »

Code: Select all

union colortype
  as ulong bgra
  type
    as ubyte b,g,r,a
  end type  
end union

type mytype
  declare constructor
  as colortype clr
end type
constructor mytype
  clr.bgra = rgba(255,255,255,255)
end constructor

dim as mytype test
sleep
Landeel
Posts: 777
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: UDT initializer : Invalid data types

Post by Landeel »

Hey Joshy, you gave me a great idea:

Code: Select all

union colortype
	bgra as uinteger<32>
	type
		b as ubyte
		g as ubyte
		r as ubyte
		a as ubyte
	end type
	declare constructor(byval bgra as uinteger<32>=rgba(255,255,255,255))
end union
constructor colortype(byval bgra as uinteger<32>)
	this.bgra=bgra
end constructor

type mytype
	clr as colortype=rgba(1,2,3,4)
end type

dim mydata as mytype

print mydata.clr.b
print mydata.clr.g
print mydata.clr.r
print mydata.clr.a
print mydata.clr.bgra
This will work like before. Thank you!
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: UDT initializer : Invalid data types

Post by paul doe »

You can even make it work transparently with rgba, too:

Code: Select all

union colortype
   bgra as uinteger<32>
   type
      b as ubyte
      g as ubyte
      r as ubyte
      a as ubyte
   end type
   declare constructor(byval bgra as uinteger<32>=rgba(255,255,255,255))
   declare constructor( as ubyte, as ubyte, as ubyte, as ubyte )
   declare operator let( as uinteger <32> )
   declare operator cast() as uinteger <32>
end union

constructor colortype(byval bgra as uinteger<32>)
   this.bgra=bgra
end constructor

constructor colortype( r as ubyte, g as ubyte, b as ubyte, a as ubyte )
  bgra = rgba( r, g, b, a )
end constructor

operator colortype.let( value as uinteger <32> )
  bgra = value
end operator

operator colortype.cast() as uinteger <32>
  return( bgra )
end operator

type mytype
  clr as colortype=rgba(1,2,3,4)
end type

var myColor = colortype( 255, 128, 64, 255 )

screenRes( 800, 600, 32 )

line( 100, 100 ) - ( 500, 500 ), myColor, bf

? myColor.r, myColor.g, myColor.b, myColor.a

sleep()
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: UDT initializer : Invalid data types

Post by MrSwiss »

The Union itself can be made in a different way (w/o all ctor/dtor stuff).

See: Color-Union evolved (over last 4 years)
Landeel
Posts: 777
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: UDT initializer : Invalid data types

Post by Landeel »

Thanks, paul doe!

I have added a constructor without parameters, so it will also work with redim:

Code: Select all

union colortype
   bgra as uinteger<32>
   type
      b as ubyte
      g as ubyte
      r as ubyte
      a as ubyte
   end type
   declare constructor(byval bgra as uinteger<32>=rgba(255,255,255,255))
   declare constructor(as ubyte, as ubyte, as ubyte, as ubyte)
   declare constructor()
   declare operator let(as uinteger <32>)
   declare operator cast() as uinteger <32>
end union

constructor colortype(byval bgra as uinteger<32>)
   this.bgra=bgra
end constructor

constructor colortype( r as ubyte, g as ubyte, b as ubyte, a as ubyte )
  bgra = rgba( r, g, b, a )
end constructor

constructor colortype()
  this.bgra = rgba(255,255,255,255)
end constructor

operator colortype.let( value as uinteger <32> )
  bgra = value
end operator

operator colortype.cast() as uinteger <32>
  return( bgra )
end operator

redim mydata2(10) as colortype

Thanks, MrSwiss. I'll take a look.
Post Reply