Author Topic: Small feature request for TYPE  (Read 1802 times)

0 Members and 2 Guests are viewing this topic.

Brian Alvarez

  • Guest
Small feature request for TYPE
« on: November 20, 2018, 01:56:10 PM »
 This is not a priority but i would like default values for types to be supported, for example:

Code: [Select]
type rgbacolor
  red   as byte = 35
  green as byte
  blue  as byte
  alpha as byte
end type

rgbacolor r

print str(r.red)

I noticed that this already compiles fine, but the value remains 0. I would also
like a way to reset to default empty values and a way to reset to predefined values.

Charles Pegge

  • Guest
Re: Small feature request for TYPE
« Reply #1 on: November 20, 2018, 04:20:16 PM »
Hi Brian,

o2 uses curly braces to assign default values:

Code: [Select]
'DEFAULT MEMBER VALUES
type color
  byte r=0x10
  byte g=0x20
  'g as byte =0x20
  byte b=0x30
  byte a=0x40
end type

color d    'sets all to null
print hex(d.r) ", " hex(d.g) ", " hex(d.b) ", " hex(d.a)

d={} 'sets all to default values
print hex(d.r) ", " hex(d.g) ", " hex(d.b) ", " hex(d.a)

d={g=0x25,b=0x35} 'default values with overrides
print hex(d.r) ", " hex(d.g) ", " hex(d.b) ", " hex(d.a)


Brian Alvarez

  • Guest
Re: Small feature request for TYPE
« Reply #2 on: November 22, 2018, 12:11:00 PM »
Awesome Charles! Thanks!

Brian Alvarez

  • Guest
Re: Small feature request for TYPE
« Reply #3 on: August 15, 2019, 09:17:07 PM »
 Sorry about reviving an old post... but i was playing with dfault values for TYPE and apparently it doesnt work when
there is a previous CHAR member. Even worse... it crashes after not setting the default values:

Code: [Select]
'DEFAULT MEMBER VALUES
type color
  char cls[200]  ' adding this makes it fail and crash.
  byte r=0x10
  byte g=0x20
  'g as byte =0x20
  byte b=0x30
  byte a=0x40
end type

color d    'sets all to null
print hex(d.r) ", " hex(d.g) ", " hex(d.b) ", " hex(d.a)

d={} 'sets all to default values
print hex(d.r) ", " hex(d.g) ", " hex(d.b) ", " hex(d.a)

d={g=0x25,b=0x35} 'default values with overrides
print hex(d.r) ", " hex(d.g) ", " hex(d.b) ", " hex(d.a)

Charles Pegge

  • Guest
Re: Small feature request for TYPE
« Reply #4 on: August 16, 2019, 08:21:36 PM »

Okay, I'm recoding this feature, including multi-assignments. It has become too complex with low level code, so I'll use recursive decomposition, and hope to save about 400 lines of o2 source code in the process :)

Brian Alvarez

  • Guest
Re: Small feature request for TYPE
« Reply #5 on: August 16, 2019, 08:57:06 PM »
Thanks charles, i also noticed it didnt work for CHAR or WCHAR elements... in fact no string types were working.