Giter Club home page Giter Club logo

Comments (6)

BenjaminNavarro avatar BenjaminNavarro commented on June 16, 2024 1

Well, for starters, C enums are a bit broken IMO. I largely prefer C++ enum classes, which are more strict by default (you have to static_cast your way to do unsafe stuff).

Regarding the string part, it would be mostly for introspection. e.g. to print all possible values to the user or finding the corresponding enumerator to a user input. Let's say you parse a configuration file where one field corresponds to an enum. Right now you either have to take numerical values (which is unsafe) or a string an maintaining a map between strings and enumerators (which is not very practical). Having a way to iterate over the enumerators and accessing their numerical value or their name would be great for these situations.

Concerning the assignment from an integer, I get it but I would prefer it to be explicit (as with static_cast) but that's maybe because I'm more into statically typed languages than dynamic ones.

from feral.

Electrux avatar Electrux commented on June 16, 2024

Well, this is in a way, inspired by the C language where enums are nothing but (usually auto assigned) numerical values.
In Feral, enums are no special types. When one declared a variable from an enum value (like you did with color), it actually just creates an integer value.
Usually, when assigning to an enum value's variable (color here), one would just use the enum itself and not assign an arbitrary integer. And when one compares an enum value variable with possible enum values, they would, again, not use the integer values directly.

At least that's how they are designed for Feral. I'd definitely like to know the benefits of printing enums as strings though 🤔

from feral.

Electrux avatar Electrux commented on June 16, 2024

Also, one thing that can be, perhaps, done is that we can have something like
enum_name.val_str(enum_var); for getting the string name of the enum value.
For your example, it would be...

let color = colors.green;
colors.val_str(color); # returns "green"

That being said, I'd still like to know the advantage of this concept with regards to enums (and their usage) 😄.

from feral.

Electrux avatar Electrux commented on June 16, 2024

That is a good idea. I'll think of something about it perhaps 🤔
It will certainly require a custom type for enums though 😅

from feral.

BenjaminNavarro avatar BenjaminNavarro commented on June 16, 2024

Take your time for this, it's not like there is an urgent need for this

from feral.

Electrux avatar Electrux commented on June 16, 2024

Haha, thanks 😄

from feral.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.