v icon indicating copy to clipboard operation
v copied to clipboard

enums

Open PavelVozenilek opened this issue 7 years ago • 7 comments

In C enums play two roles: collection of symbols, and a grouping of constants (slightly better than mere list of const). There's lot of magic involved: implicit initial numerical value, automatic increments, implicit cast to int.

OTOH iota is missing. (Also switch doesn't require to handle exhaustively all possibilities and the names of enum values spill out and pollute everything.)

What V could do is to separate these two use cases, without any extra syntax.


1] Enums which are collection of symbols would look like:

enum States { Japan, Australia, Spain, Portugal }

These enum values would have no accessible numerical value. It would be possible to compare them for equality/inequality, use in switch (exhaustively), but not extract a number.

println of such enum value would print out stringified symbol (or perhaps it should be forbidden).

Also the comma between enum values is not really needed:

enum States { Japan Australia Spain Portugal }


2] Enums which are handy collection of constants. They would require explicit numeric initialization for every value:

enum Months { 
  January   = 1
  February = 2
  March = February + 1
  ...
}

In this case number could be extracted out of the enum. These enums could be compared to each other (and to integers) by <, >, <=, >=, ==, !=.

These enums may expose limits: Months.min (== 1) and Months.max (== 12).

Floating point numerical values may be also considered.

By default the numeric base type of enum would be int. It should be possible to specify different base:

enum i8(Months) { 
  ...
}

Months.base-type would be equal to i8.

I would avoid iota, as this is trying to hide the very basic information.

PavelVozenilek avatar Apr 05 '19 23:04 PavelVozenilek

Sounds good to me.

medvednikov avatar Apr 07 '19 01:04 medvednikov

These enum values would have no accessible numerical value

There needs to be a way of accessing the numeric value, at least by int(Enum.member).

collection of constants... require explicit numeric initialization for every value

If you want to require initialization, you can probably use const declarations (this doesn't put declarations behind a group namespace though).

I think having two kinds of enum isn't worth the complication, and actually limits what the programmer might rightly want to do, e.g. define enum E {foo = 7, bar, baz}. Why shouldn't we be able to start at an arbitrary value?

the comma between enum values is not really needed

If (optional) initializers are supported (only one kind of enum), I think we should require commas [or newlines] to help prevent accidental typos. (Sometimes redundancy in syntax is useful for humans, compiler parsing complexity and more accurate error messages).

enum i8(Months)

I suggest enum Months i8 {}. That seems more consistent.

ntrel avatar Apr 09 '19 14:04 ntrel

If you want to require initialization, you can probably use const declarations

consts and enums could be merged into one feature. I'd prefer const keyword:

const {
  Japan Brazil // global symbols, spill out
}

const foo {
  Korea Bhutan // global symbols which needs the prefix foo
}

const {
  PI = 3.14 // global constants
  e = 2.78
}

const trigonometry {
 SinPi/4 = ...
}

// inside a function
fn bar() 
{
   const { red green blue }
   const x = 1
   const baz { ... }
}

PavelVozenilek avatar Apr 10 '19 20:04 PavelVozenilek

I think const and enum are different things. const allows symbols in the same block to have different types, because of this it's less obvious the constants need to be namespaced. enum auto-increments when an expression is missing.

ntrel avatar Apr 11 '19 11:04 ntrel

I agree that const id = expr inside a function is useful, particularly in generic code. I'm not sure enum is that useful inside a function.

ntrel avatar Apr 11 '19 11:04 ntrel

I agree with @ntrel that enum values should always have a numerical value associated with them. This is typical enum behavior in a lot of languages and simplifies things across the board.

watzon avatar Jul 13 '19 05:07 watzon

Option 1 is now a real thing:

enum test_enum {
    one
    two
    three
    four
}

Bowero avatar Aug 26 '19 20:08 Bowero