Why can't an Enum be an unsigned integer type?

I wanted to define an Enum as an Uint8 (or Byte) but the IDE won’t let me… but it will let me define it as an int8…

Is that intended behavior? If so why are not unsigned integer types not supported… It’s not like we can easily do math with enums!

  • karen

That is most likely the reason. Enumerations are more like a coding comodity than a real data type.

If you need “real values”, use Extends and Assigns in a module.

On Windows there are enumerations which have both negative and positive values.

Why would that matter?

The IDE already lets us specify the Enum data type (as long as it is SIGNED integer type).

If it allowed Unsigned types one could still use a signed integer type for those and of course the default type could stay what it is now which is just INTEGER.

Hmmm maybe it is because when enums were introduced the unsigned types did not exist… ( IIRC RB did not have Enums or unsigned types when I first started using it, but I don’t remember whatever features were added.)

BTW speaking of Datatype issues, Variant.Type code as listed in the docs does not support all the integer types…
This is the first time I wanted that and never noticed that were missing before…

Assign a byte to a variant and variant.type will say you assigned an Int32…

This is the type of stuff that really annoys me and wastes a lot of time for me

I come up with a design to handle something in a general manor that logically should work and start coding, but then I am often stymied, delayed or have to compromise functionality because of an incomplete or buggy Xojo feature implementation.

-Karen

Too much!

In xojo, the variant does not behave as a “container”, it does some conversion. Primitives are not intended to carry more than its intrinsic type of data, maybe you need to use a class.

But, if you already have some code on your idea, maybe using Auto instead Variant and Introspection.GetType instead of VarType

An interesting perspective on programming, but one that is doomed to fail… A design that fails to take account of the limitations of the implementation language is not really a design in my book, more of a dream really.

The use of constants in calculations is a widely used concept in most languages, but the use of enums is not something i’ve seen advocated anywhere.

[quote=437466:@James Dooley]An interesting perspective on programming, but one that is doomed to fail… A design that fails to take account of the limitations of the implementation language is not really a design in my book, more of a dream really.
[/quote]

Of course I take into account what Xojo can do…once I trip over the limitations…

Too often it falls short of logical/expected(even from documentation)/intuitive reasonable expectations.

I tend to be a through detail oriented person …and the mainframe/mini computer languages i taught myself on back in the day were like that too… and it is what I expect from a professional tool.

It’s not that there were no bugs or shortcomings back then … but language features tended to implemented much more completely/throughly … and that is how it should be.

[quote]
The use of constants in calculations is a widely used concept in most languages, but the use of enums is not something i’ve seen advocated anywhere.[/quote]

Depends on the language. In Xojo I see them not only as a way of limiting possible values, but also for code/project organization and help keeping autocomplete lists from becoming too long by showing only relevant options.

  • karen

[quote=437436:@Karen Atkocius]I wanted to define an Enum as an Uint8 (or Byte) but the IDE won’t let me… but it will let me define it as an int8…
[/quote]
Works fine here to change the enum type to pretty much any integer type in 2019r1

EDIT : in fact t works in XojoScript as well (which is unsurprising)

class myclass
  Enum foo As UInt8
  foovalue1
  foovalue2
  End Enum
  
  Dim i As foo
  
End Class

Dim c As New myClass

c.i = myclass.foo.foovalue2

Select Case c.i
Case myclass.foo.foovalue1
  Print "myclass.foo.foovalue1"
Case myclass.foo.foovalue2
  Print "myclass.foo.foovalue2"
End Select

If you add this to an IDE script and press run you get a result printed
If you change the Enum type to “String” or some other non-integer type you get a compile error

it works, and you can even get the correct type

  Enum foo As UInt8
  foovalue1
  foovalue2
  End Enum


Dim x As Auto = foo.foovalue1

Dim info As Xojo.Introspection.TypeInfo
info = Xojo.Introspection.GetType (x)

Select Case Info.FullName
Case "UInt8"
  beep
Case Else
  'Is Not a UInt8
End Select


Break

I was confused by IDE bugs.

I was using 2018R3 and there If I typed say UINT8 into the datatype field for an enum and hit return, it immediately changed back to integer… If instead click on the name field it stayed as UINT8 but the naviagator displayed it as integer… But inspecting it showed it to be UINT8

Just tried it in 2019R1 and the above issues are fixed BUT if I copy an Enum of type UINT8 and past it into another object the type changes to integer and that goes BOTH for the IDE and the inspector

  • Karen

its entirely possible there are copy paste bugs that I did not fix :stuck_out_tongue:
I suspect its simply that what gets copied doent include the type as a paste into text editor shows

Public Enum Untitled
valueName
valueName1
End Enum

Note there is no AS TYPE so the default type is Integer

Thats probably worth a bug report

EDIT : <https://xojo.com/issue/55733>