Enums and array of specific types
-
Hi, guys, it has been a while since i came here,
So i was working on a text mode game that i ended up totally rewriting on my PC using C, and now i am rewriting in fuze project from scratch.
For the game i need some structs to represent tiles and related objects.
I am wondering: how to implement enums in fuze? I need those and i really can't find anything about them on the forum, in case those are not a feature in fuze (yet) i will be glad to you if you point out possible alternatives, i want to absolutely avoid cluttering my code with global variables just to keep constant values for this purpose.
Also other question: is if it's possible to create arrays of fixed size made out of objects of the same type? This can be a huge optimization helper since i need to have a map stored in memory (which is an array of 256*256 integers) and other arrays to store all of the tiles, npc dialogs, triggerable actions, enemies, etc ... which are all structures
That said thank you and keep up the good work.
-
I just had to google for ’enum’, and it seems it’s just a named constant? What am I missing here?
-
Yes it is a defined set of constants that you can reference by name rather than value, a bit like the colours in Fuze. Except of course that you can define them yourself
-
No, enums in modern programming are objects with a fixed number of states which are all declared with the enum itself, for example in swift (use it as a reference because it has a good implementation of enums) you can do something like this:
enum Size{
case small
case medium
case large
}var myThing = small
mything = large //we can do this
mything = [some other stuff] //we can't do this, a Size obejct has just 3 possible values which are: small, medium and large
as you can see we have created an object that can have just 3 states and accepts no other possible values, this is very usefoul when you have to create something that has just those values you need, because it frees you from handleing edge cases and invalid values like with integer costants and makes the code more simple to maintain
-
Fuze is loosely typed. It also doesn't really have any features that help you refactor code (as in: if you rename the property of a struct you will have to manually change the name of that property anywhere you use it). So the added value for Enums in the language is readability at most.
You could use a struct in the following manner:
Size = [ .small = 1, .medium = 2, .large = 3 ] var myThing = Size.small myThing = Size.large
However, Fuze would still allow you to do:
myThing = [ "some other stuff"] myThing = "more stuff" myThing = false
-
@PB____ Yours seems to be a pretty reasonable alternative to enums, also because i have a syntax similar to the one used by modern programming languages, i think i will use your solution for now.
-
@toxibunny in strictly typed languages (e.g. like java, kotlin, c++, etc.) enums are great to achieve the domain definition. E.g. you know that the value passed to certain method can only be one of {EMPLOYEE, CONTRACTOR, ...} but nothing else. Enum make sure that whtever is passed int he method has this contraint at 1) compile time where possible 2) in case the data is fetched from external source and e.g. deserialized into enum the runtime error is thrown if they won't fit inside the domain defined. Well apprarently in dynamic typed language this doesnt' make too much of sense from a laguage protection level and it's used just to make the code more clean (as suggested with the struct above)