Does BlitzMax have ENUMs?

BlitzMax Forums/BlitzMax Beginners Area/Does BlitzMax have ENUMs?

Difference(Posted 2005) [#1]
Does BlitzMax have ENUMs, like:
Enum
    John
    Billy
    Betty
End Enum

If not, can we have them? :)


Robert(Posted 2005) [#2]
Not as far as I am aware, but I can see that this would be useful (and probably pretty easy to implement as well)


Warren(Posted 2005) [#3]
I would vote for enums as well. I currently have an enum I've had to fake with "const" lines and since it contains over 100 items, manually numbering each line really sucked.

It should be easy enough to get the compiler preprocessor to swap out the enums and create const lines internally. Right?


bradford6(Posted 2005) [#4]
what is the output, what does "enum" do?


Robert(Posted 2005) [#5]
Enum
   Paul
   John
   Mary
End Enum


Would be the same as:

    Const Paul=0
    Const John=1
    Const Mary=2



Warren(Posted 2005) [#6]
It would definitely need the ability to start numbering manually as well. Like:

Enum
    Paul = 50
    John
    Mary
End Enum


Results in:

Paul = 50
John = 51
Mary = 52


Mark Tiffany(Posted 2005) [#7]
what is the output, what does "enum" do?

It specifies a 'group' of constants. For example, if you have a function called CreateAlien(type), there are only going to be a set number of possible inputs that you support. A neat way of encapsulating that (and validating the inputs) is to use enums.

For example:

Enum AlienTypes
Alien=1
Predator=2
End Enum

Function CreateAlien(type:AlienTypes)
...
End function

CreateAlien(Alien) ' works
CreateAlien(3) ' doesn't work

Having said that, they're not something I miss enormously, although they are seriously handy if your IDE has intellisense, as you would see a dropdown of the valid enums to select from...


teamonkey(Posted 2005) [#8]
It's just syntactic sugar, but it'd be nice to see enums in a future update.


Warren(Posted 2005) [#9]
It's more than sugar. As per the example, I posted - I have over 100 elements grouped together and I have to manually number each one. If I insert a new one at item 25, I have to manually change the values on the following 75 items. It sucks.

Enums would handle that automagically.


Beaker(Posted 2005) [#10]
I would like Enum as well.


flying willy(Posted 2005) [#11]
I am unconvinced.

I'd like to know what the real benefits of enums are? surely it doesn't save on typing. A constant is a constant and there's bound to be more elegent ways than Enums from what I can see.


Murilo(Posted 2005) [#12]
Enums really come into there own with intellisense IDEs imho.

I vote for enums too.


Robert(Posted 2005) [#13]
I'd like to know what the real benefits of enums are? surely it doesn't save on typing. A constant is a constant and there's bound to be more elegent ways than Enums from what I can see.


The advantage don't have to provide a value for each constant, the compiler does it for you. It also allows the IDE to show a list of appropriate enums whilst you are typing.

It does save typing, no need to put "Const" and "=[Literal Value]" on each line - I suppose that is why it is useful.


Warren(Posted 2005) [#14]
skunk

- Ease of numbering lists of items
- Intellisense possibilities once a decent IDE is available
- Can be converted to const items by the preprocessor so they are effectively the same thing but with much less programmer effort
- Can be type checked when being passed into functions

What's not to like?


fredborg(Posted 2005) [#15]
Sounds like a solid idea. I'm thinking WarrenMs idea would be better, if it went like this:
Enum 50 'Begin from 50
Jack
Jim
Joe
EndEnum
Perhaps even this:
Enum 50 Step 2 'Begin from 50 and add 2 for each line, ie. 50, 52, 54, etc.
Jack
Jim
Joe
EndEnum
I don't know if that would be useful?


Warren(Posted 2005) [#16]
Ahh, but then you can't do this:

Enum
    John = 50
    Jack
    Mary
    Tom = 100
    Dick
    Harry
End Enum


I can't see the stepping being useful, but if it's easy to add ... sure, why not.


fredborg(Posted 2005) [#17]
How very true! It was just an idea, I can't see it being useful either :)


bradford6(Posted 2005) [#18]
this took 3 minutes. no big deal. Use Excel or Open Office calc to arrange your columns. Enum may be nice but this is more powerful.

Const		fred	=	1
Const		mary	=	2
Const		george	=	3
Const		sally	=	4
Const		john	=	5
Const		carl	=	6
Const		patty	=	7
Const		tim	=	8
Const		toad	=	9
Const		food	=	10
Const		perry	=	11
Const		minas	=	12
Const		dog	=	13
Const		foop	=	14
Const		carol	=	15
Const		pino	=	16
Const		greg	=	17
Const		will	=	18
Const		bill	=	19
Const		tom	=	20
Const		thomas	=	21
Const		deirdre	=	22
Const		frank	=	23
Const		lyle	=	24
Const		bfred	=	25
Const		bmary	=	26
Const		bgeorge	=	27
Const		bsally	=	28
Const		xjohn	=	29
Const		xcarl	=	30
Const		xpatty	=	31
Const		xtim	=	32
Const		xtoad	=	33
Const		xfood	=	34
Const		xperry	=	35
Const		xminas	=	36
Const		xdog	=	37
Const		xfoop	=	38
Const		xcrap	=	39
Const		xpino	=	40
Const		xgreg	=	41
Const		xwill	=	42
Const		xbill	=	43
Const		xtom	=	44
Const		xthomas	=	45
Const		xdeirdre	=	46
Const		qfrank	=	47
Const		qlyle	=	48
Const		qfred	=	49
Const		qmary	=	50
Const		qgeorge	=	51
Const		qsally	=	52
Const		qjohn	=	53
Const		qcarl	=	54
Const		qpatty	=	55
Const		qtim	=	56
Const		qtoad	=	57
Const		qfood	=	58
Const		qperry	=	59
Const		qminas	=	60
Const		qdog	=	61
Const		qfoop	=	62
Const		qcrap	=	63
Const		qpino	=	64
Const		qgreg	=	65
Const		qwill	=	66
Const		qbill	=	67
Const		qtom	=	68
Const		zthomas	=	69
Const		zdeirdre	=	70
Const		zfrank	=	71
Const		zlyle	=	72
Const		zfred	=	73
Const		zmary	=	74
Const		zgeorge	=	75
Const		zsally	=	76
Const		zjohn	=	77
Const		zcarl	=	78
Const		zpatty	=	79
Const		ztim	=	80
Const		ztoad	=	81
Const		zfood	=	82
Const		zperry	=	83
Const		zminas	=	84
Const		zdog	=	85
Const		zfoop	=	86
Const		zcraptastic	=	87
Const		zpino	=	88
Const		zgreg	=	89
Const		wzill	=	90
Const		bzzill	=	91
Const		tzom	=	92
Const		pthomas	=	93
Const		pzdeirdre	=	94
Const		pzfrank	=	95
Const		pzlyle	=	96
Const		pzfred	=	97
Const		pzmary	=	98
Const		pzgeorge	=	99
Const		pzsally	=	100



Warren(Posted 2005) [#19]
You're kidding me ... right? You want me to use Excel to manage lists of pseudo enums? Good grief.


flying willy(Posted 2005) [#20]
I'd just use vars instead of consts... no big deal. Appreciate the explanation though.


N(Posted 2005) [#21]
Here's a way to fake it:

Type PrimitiveType Final
     Const TriangleList = 1
     Const QuadList = 2
     Const Polygon = 3
     Const TriangleStrip = 4
     Const TriangleFan = 5
End Type


But anyways, I'd much rather prefer built-in support for enumerators.


teamonkey(Posted 2005) [#22]
@bradford6
What happens if you want to add another constant between "perry" and "minas"?


Matthew Smith(Posted 2005) [#23]
Count me in as wanting them - there very handy indeed!

You need to point out the actual use of enums (as outlined, it's great with intellisense), but it does allow you to structure your code nicely (as any VB or VB.Net user would know):

Enum Primitive
   Triangle = 1
   Quad = 2
   Polygon = 3
   TriangleStrip = 4
   TriangleFan = 5
End Enum


It could then be used as follows:

Function GetPrimative:String(PrimType:Primitive)
   
   Select PrimType
      Case Primitive.Triangle
          Return "Triangle"

       Case Primitive.Quad
          Return "Quad"

      Case Primitive.Polygon
          Return "Polygon"

      Case Primitive.TriangleStrip
          Return "TriangleStrip"
    
      Case Primitive.TriangleFan
          Return "TriangleFan"
      
      Case Default
          Return "Unknown"

   End Select

End Function


Print GetPrimitive(Primitive.Triangle)


@teamonkey
Your point about adding other constants is easily done without affecting the current list (or references like Select Case statements as above) - the actual integer value is irrelevant as you refer to the name, not it's value - if you get my drift.

Once you get the hang of it, it makes your code so much more readable.

@bradford
Enums are certainly much more flexible (and powerful) than consts if used correctly. The list you have created could apply to anything - enums can be much more specific.


Brucey(Posted 2005) [#24]
I'm not sure I understand why you would want to insert something in the middle of a list of enumerations and *then* renumber the following entries?

Given the whole idea of enumeration is that you don't care what values they are (generally), why not just add them on the end?

:-)


Curtastic(Posted 2005) [#25]
Of course blitzmax should have this feature but I never found a reason to use consts. I always did:
Type Alien
Field class:AlienType
EndType

Type AlienType
EndType
Global Alien:AlienType
Global Predator:AlienType

Function CreateAlien(type:AlienType)
...
a=new alien
a.class=type
...
End function

CreateAlien(Alien) ' works
CreateAlien(3) ' doesn't work

it also catches errors in B3D like predayor with "type mismatch"


SJB(Posted 2005) [#26]
Given the whole idea of enumeration is that you don't care what values they are (generally), why not just add them on the end?


Sometimes you don't care what the values are, sometimes you do. Your enums may represent an ordered list of values.

The advantage of enums is that you are giving a descriptive name to a constant value, so you don't see 'magic numbers' in your code. The other big advantage of try enums is that each enum is a specific type (just like a class or BlitzMax Type). The compiler can then check the type of the arguments to functions if they are defined to take the enum.


innerspace(Posted 2005) [#27]
I vote for enums too.


Beaker(Posted 2005) [#28]
Something I do occasionally is to bitwise combine consts, so instead of the Enum list running from 1 to whatever it would increase in a bit friendly manner, like this:
Enum Bitwise
    John  'gives 1
    Jack  'gives 2
    Mary  'gives 4
    Tom   'gives 8
    Dick  'gives 16
    Harry 'gives 32 etc
End Enum



Warren(Posted 2005) [#29]
Beaker

Any chance of FONText for Mac? :) There's definitely a market there ... I've searched high and low for a decent bitmap font maker and have come up dry.


Beaker(Posted 2005) [#30]
Maybe. I will definitely be doing a lib to draw FONText text.


Warren(Posted 2005) [#31]
Well, drawing the characters is easy. It's generating the nice fonts that is the bottleneck...