Re: Can Enums be integral types?

2022-04-19 Thread Manfred Nowak via Digitalmars-d-learn
On Sunday, 17 April 2022 at 18:25:32 UTC, Bastiaan Veelo wrote: The reason is in [17.1.5](https://dlang.org/spec/enum.html): “EnumBaseType types cannot be implicitly cast to an enum type.” Thy. That's the anchor in the specs preventing Enums to be integral types.

Re: Can Enums be integral types?

2022-04-19 Thread Era Scarecrow via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 13:20:21 UTC, Bastiaan Veelo wrote: There is nothing that requires enum values to be unique, though: ```d import std; void main() { enum E {Zero = 0, One = 0, Two = 0} writeln(E.Two); // Zero! } ``` True, but if you want it be useful they really need to be

Re: Can Enums be integral types?

2022-04-19 Thread Bastiaan Veelo via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 01:25:13 UTC, Era Scarecrow wrote: The 'integral' or numeric value is used for uniqueness, […] There is nothing that requires enum values to be unique, though: ```d import std; void main() { enum E {Zero = 0, One = 0, Two = 0} writeln(E.Two); // Zero! } ```

Re: Can Enums be integral types?

2022-04-18 Thread Era Scarecrow via Digitalmars-d-learn
On Sunday, 17 April 2022 at 18:25:32 UTC, Bastiaan Veelo wrote: On Saturday, 16 April 2022 at 11:39:01 UTC, Manfred Nowak wrote: In the specs(17) about enums the word "integral" has no match. But because the default basetype is `int`, which is an integral type, enums might be inte

Re: Can Enums be integral types?

2022-04-17 Thread Bastiaan Veelo via Digitalmars-d-learn
On Saturday, 16 April 2022 at 11:39:01 UTC, Manfred Nowak wrote: In the specs(17) about enums the word "integral" has no match. But because the default basetype is `int`, which is an integral type, enums might be integral types whenever their basetype is an integral type. On the

Can Enums be integral types?

2022-04-16 Thread Manfred Nowak via Digitalmars-d-learn
In the specs(17) about enums the word "integral" has no match. But because the default basetype is `int`, which is an integral type, enums might be integral types whenever their basetype is an integral type. On the other hand the specs(7.6.5.3) about types say | A bool value can be

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 00:57:27 UTC, Paul Backus wrote: ```d enum instantiate(string type, string expr) = type ~ "(" ~ expr ~ ")"; pragma(msg, instantiate!("RVector!(SEXPTYPE.REALSXP)", "x")); ``` One possibility is to generate a collection of compile time strings that denote the

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 00:42:18 UTC, data pulverizer wrote: On Tuesday, 28 December 2021 at 00:32:03 UTC, Paul Backus wrote: In this case, the simplest solution is to have your code generator accept a string as its input, rather than a type. For example: ```d enum instantiate(string

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 00:32:03 UTC, Paul Backus wrote: The result of `.stringof` is completely implementation-defined, may change arbitrarily between compiler releases, and is not even guaranteed to be valid D code in the first place. Wow, I didn't know this. In this case, the simpl

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread Adam Ruppe via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 00:13:13 UTC, data pulverizer wrote: There are various requirements, sometimes I have to cast or type convert, so I **need** the type to paste correctly and explicitly. You almost never actually need types as strings. I'm almost certain there's a better way for

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 28 December 2021 at 00:13:13 UTC, data pulverizer wrote: The types I'm generating are a template type I've constructed for R's SEXP, so that my wrapped numeric vector (struct) type is denoted `RVector!(REALSXP)`. But `alias REALSXP = SEXPTYPE.REALSXP` where `SEXPTYPE` is an `enum`.

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
On Monday, 27 December 2021 at 23:04:40 UTC, Adam Ruppe wrote: On Monday, 27 December 2021 at 21:21:30 UTC, data pulverizer wrote: alias T = MyType!(INTEGER); What is MyType? enum code = "writeln(\"instance: \", adder(" ~ T.stringof ~ "(), " ~ U.stringof ~ "()" ~ "));"; A

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread Adam Ruppe via Digitalmars-d-learn
On Monday, 27 December 2021 at 21:21:30 UTC, data pulverizer wrote: alias T = MyType!(INTEGER); What is MyType? enum code = "writeln(\"instance: \", adder(" ~ T.stringof ~ "(), " ~ U.stringof ~ "()" ~ "));"; And why is this a string mixin instead of a plain simple function

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
On Monday, 27 December 2021 at 22:52:58 UTC, data pulverizer wrote: I think the only thing to do for now is probably for me to construct a template that creates a proper string for this type. It would look something like this: ``` enum safe_stringof(T) = T.stringof; template safe_stringof(T: M

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
On Monday, 27 December 2021 at 21:31:03 UTC, Adam Ruppe wrote: if you can paste teh code where you generate this I can prolly show you a much easier way to do it. stringof sucks really hard. I think the only thing to do for now is probably for me to construct a template that creates a proper s

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
On Monday, 27 December 2021 at 21:31:03 UTC, Adam Ruppe wrote: if you can paste teh code where you generate this I can prolly show you a much easier way to do it. stringof sucks really hard. Will the above `mixin` example suffice? It expands to the code that I described.

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread Adam Ruppe via Digitalmars-d-learn
On Monday, 27 December 2021 at 21:05:51 UTC, data pulverizer wrote: adder(MyType!MyEnum.INTEGER(), MyType!MyEnum.STRING()); The rule for !(args) is of you leave the parenthesis off, it only uses the next single token as the argument. So it will never include a dot; it is like you wrote `MyTyp

Re: Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
On Monday, 27 December 2021 at 21:05:51 UTC, data pulverizer wrote: Hello, ... ... an equivalent mixin error would be ``` //... alias DOUBLE = MyEnum.DOUBLE; alias STRING = MyEnum.STRING; alias INTEGER = MyEnum.INTEGER; void main() { alias T = MyType!(INTEGER); alias U = MyType!(STRING)

Ambiguity issue with expanding and evaluating single template type parameter enums

2021-12-27 Thread data pulverizer via Digitalmars-d-learn
Hello, I'm generating code using mixins and one of my mixins expands to something like this: ``` adder(MyType!MyEnum.INTEGER(), MyType!MyEnum.STRING()); ``` `MyType!MyEnum.STRING` is generated with `T.stringof `. I get the error: ``` Error: template instance `MyType!(MyEnum)` does not matc

Re: Why are enums with base type string not considered strings?

2021-03-16 Thread wolframw via Digitalmars-d-learn
hard to disagree with. So, perhaps the better solution would be to make isBoolean and isSomeChar (and perhaps other functions that I didn't think of) return false for enums? As a side note, isSomeChar returning true for enums is also what causes the behavior demonstrated in Issue 2163

Re: Why are enums with base type string not considered strings?

2021-03-14 Thread Bastiaan Veelo via Digitalmars-d-learn
On Sunday, 14 March 2021 at 16:09:39 UTC, Imperatorn wrote: On Sunday, 14 March 2021 at 10:42:17 UTC, wolframw wrote: enum BoolEnum : bool { TestBool = false } enum CharEnum : char { TestChar = 'A' } enum StringEnum : string { TestString = "Hello" } pragma(msg, isBoolean!BoolEnum);

Re: Why are enums with base type string not considered strings?

2021-03-14 Thread Imperatorn via Digitalmars-d-learn
rEnum); // true pragma(msg, isSomeString!StringEnum); // false Why does isSomeString not return true for an enum with base type string while other isX functions return true for enums with an according base type X? Regarding whether enums should be considered by these functions, I can see

Why are enums with base type string not considered strings?

2021-03-14 Thread wolframw via Digitalmars-d-learn
// false Why does isSomeString not return true for an enum with base type string while other isX functions return true for enums with an according base type X? Regarding whether enums should be considered by these functions, I can see the case being made one of both ways (personally, I'd

Does UDA not work for enums?

2018-01-01 Thread Marc via Digitalmars-d-learn
I got compilers errors from this: enum E { @("foo") A, @("baa") B } I got: Error: basic type expected, not @ Error: no identifier for declarator _error_ Error: type only allowed if anonymous enum and no enum type Error: if type, there must be an initializer Er

Re: Does UDA not work for enums?

2018-01-01 Thread Seb via Digitalmars-d-learn
On Monday, 1 January 2018 at 17:15:24 UTC, Marc wrote: I got compilers errors from this: enum E { @("foo") A, @("baa") B } I got: Error: basic type expected, not @ Error: no identifier for declarator _error_ Error: type only allowed if anonymous enum and no e

Re: Passing anonymous enums as function parameters

2017-12-20 Thread kerdemdemir via Digitalmars-d-learn
enum { a = "foo", b = "bar", c = "baz"; } is identical to enum a = "foo"; enum b = "bar"; enum c = "baz"; Thanks Jonathan I think that changes my point of perspective. And Jacob Carlborg I like the third option a lot with aliases good to know that enum Foo : string { KErde

Re: Passing anonymous enums as function parameters

2017-12-18 Thread Jacob Carlborg via Digitalmars-d-learn
On 2017-12-17 20:45, Jonathan M Davis wrote: That's pretty much just declaring manifest constants with braces so that you don't repeat the keyword enum a bunch of times. Anonymous enum is what the spec calls it and was available before manifest constants. -- /Jacob Carlborg

Re: Passing anonymous enums as function parameters

2017-12-17 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, December 17, 2017 12:47:26 kerdemdemir via Digitalmars-d-learn wrote: > What I meant with anonymous enums was: > https://dlang.org/spec/enum.html#anonymous_enums. Maybe I > couldn't explain well but I believe D have anonymous enums. I am > sorry I have forgotten to rem

Re: Passing anonymous enums as function parameters

2017-12-17 Thread Jacob Carlborg via Digitalmars-d-learn
s and be able to call functions with different enumarations. Or is there any other way to call named enums without type name ? You have three options: * Specify "string" as the type of the parameter for the ReturnCoolNess function. Note that this will allow any string to be passed t

Re: Passing anonymous enums as function parameters

2017-12-17 Thread kerdemdemir via Digitalmars-d-learn
What I meant with anonymous enums was: https://dlang.org/spec/enum.html#anonymous_enums. Maybe I couldn't explain well but I believe D have anonymous enums. I am sorry I have forgotten to remove " :string" in my example from the "enum : string". Please stretch out &q

Re: Passing anonymous enums as function parameters

2017-12-17 Thread Jonathan M Davis via Digitalmars-d-learn
e Ali: > { > return 100.0; > } > case Salih: > { > return 100.0; > } > // etc.. >} > } > > Is there any way I still keep my enum anonymous and be able to > call functions with different enumarations. Or is there any other > way to call named enums

Passing anonymous enums as function parameters

2017-12-17 Thread kerdemdemir via Digitalmars-d-learn
ymous and be able to call functions with different enumarations. Or is there any other way to call named enums without type name ?

Re: How to correctly generate enums at compile time.

2017-04-30 Thread jkpl via Digitalmars-d-learn
On Sunday, 30 April 2017 at 22:03:02 UTC, Kevin Balbas wrote: On Sunday, 30 April 2017 at 21:31:22 UTC, jkpl wrote: On Sunday, 30 April 2017 at 21:13:07 UTC, Kevin Balbas wrote: On Sunday, 30 April 2017 at 20:58:36 UTC, jkpl wrote: On Sunday, 30 April 2017 at 20:05:59 UTC, Kevin Balbas wrote:

Re: How to correctly generate enums at compile time.

2017-04-30 Thread Kevin Balbas via Digitalmars-d-learn
On Sunday, 30 April 2017 at 21:31:22 UTC, jkpl wrote: On Sunday, 30 April 2017 at 21:13:07 UTC, Kevin Balbas wrote: On Sunday, 30 April 2017 at 20:58:36 UTC, jkpl wrote: On Sunday, 30 April 2017 at 20:05:59 UTC, Kevin Balbas wrote: Strangely enough, it does work fine in the test snippet, As

Re: How to correctly generate enums at compile time.

2017-04-30 Thread jkpl via Digitalmars-d-learn
On Sunday, 30 April 2017 at 21:13:07 UTC, Kevin Balbas wrote: On Sunday, 30 April 2017 at 20:58:36 UTC, jkpl wrote: On Sunday, 30 April 2017 at 20:05:59 UTC, Kevin Balbas wrote: Strangely enough, it does work fine in the test snippet, As well if you import the snippet in another module. That'

Re: How to correctly generate enums at compile time.

2017-04-30 Thread Kevin Balbas via Digitalmars-d-learn
On Sunday, 30 April 2017 at 20:58:36 UTC, jkpl wrote: On Sunday, 30 April 2017 at 20:05:59 UTC, Kevin Balbas wrote: I've got the following code snippet, which almost does what I want. struct TaggedType {} @TaggedType struct Foo {} @TaggedType struct Bar {} string GenerateTypeEnum() { st

Re: How to correctly generate enums at compile time.

2017-04-30 Thread jkpl via Digitalmars-d-learn
On Sunday, 30 April 2017 at 20:05:59 UTC, Kevin Balbas wrote: I've got the following code snippet, which almost does what I want. struct TaggedType {} @TaggedType struct Foo {} @TaggedType struct Bar {} string GenerateTypeEnum() { string enumString = "enum TypeEnum {"; foreach (name;

How to correctly generate enums at compile time.

2017-04-30 Thread Kevin Balbas via Digitalmars-d-learn
I've got the following code snippet, which almost does what I want. struct TaggedType {} @TaggedType struct Foo {} @TaggedType struct Bar {} string GenerateTypeEnum() { string enumString = "enum TypeEnum {"; foreach (name; __traits(allMembers, mixin(__MODULE__))) { import

Re: Enums and immutables

2017-03-18 Thread ag0aep6g via Digitalmars-d-learn
On 03/18/2017 01:22 PM, Oleg B wrote: enum arr = cast(ubyte[])[0,0,0,1,0,0,0,2,0,0,0,3,0,0,0,4]; auto arr1 = cast(void[])arr; immutable arr2 = cast(immutable(void)[])arr; enum arr3 = cast(void[])arr; Aside: The casts here do nothing to affect the outcome. writeln(cast(ush

Re: Enums and immutables

2017-03-18 Thread rikki cattermole via Digitalmars-d-learn
ation of enums by compiler as #define. It's right? It's behavior by design? It took me a bit but what I thinking happening is 1 and 2 are being casted at runtime where as 3 is at CT. At CT its per value of the enum array, at RT its per x bytes from array. Which sort of makes sense a

Enums and immutables

2017-03-18 Thread Oleg B via Digitalmars-d-learn
; writeln(cast(ushort[])arr1); // [0, 256, 0, 512, 0, 768, 0, 1024] writeln(cast(ushort[])arr2); // [0, 256, 0, 512, 0, 768, 0, 1024] writeln(cast(ushort[])arr3); // [0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4] } I think it's related to representation of enums by compiler as #d

Re: Intelligent enums

2017-01-18 Thread Nicholas Wilson via Digitalmars-d-learn
On Thursday, 19 January 2017 at 03:47:34 UTC, Ignacious wrote: On Thursday, 19 January 2017 at 02:59:04 UTC, Ignacious wrote: I have the need to create an enum flag like structure to specify certain properties of a type easily. e.g., enum properties { Red, Blue, Hot, Sexy, Acti

Re: Intelligent enums

2017-01-18 Thread Ignacious via Digitalmars-d-learn
On Thursday, 19 January 2017 at 02:59:04 UTC, Ignacious wrote: I have the need to create an enum flag like structure to specify certain properties of a type easily. e.g., enum properties { Red, Blue, Hot, Sexy, Active, ... } But some properties will be mutually exclusive. I

Intelligent enums

2017-01-18 Thread Ignacious via Digitalmars-d-learn
I have the need to create an enum flag like structure to specify certain properties of a type easily. e.g., enum properties { Red, Blue, Hot, Sexy, Active, ... } But some properties will be mutually exclusive. I would like to contain all those rules for in the enum itself fo

Re: Some strange behaviors of enums and string.startsWith

2016-04-08 Thread Andre via Digitalmars-d-learn
On Friday, 8 April 2016 at 14:56:51 UTC, Adam D. Ruppe wrote: On Friday, 8 April 2016 at 14:38:10 UTC, Andre wrote: Therefore I use std.conv.text to convert the string enum? to string. That converts the *name* of the enum to string, not the contents. (BTW, I think the name of the enum is actu

Re: Some strange behaviors of enums and string.startsWith

2016-04-08 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 8 April 2016 at 14:38:10 UTC, Andre wrote: Therefore I use std.conv.text to convert the string enum? to string. That converts the *name* of the enum to string, not the contents. (BTW, I think the name of the enum is actually the more useful behavior.) Use cast(string) if you want

Some strange behaviors of enums and string.startsWith

2016-04-08 Thread Andre via Digitalmars-d-learn
Hi, I have some issues with enums. Please have a look at the last 3 assertions. It is annoying that I cannot directly use my StringEnum for startsWith. Therefore I use std.conv.text to convert the string enum? to string. But then the assertion fails, that is very strange, it fails only for

Re: static init of associative array of enums not working.. why?

2016-02-26 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 27 February 2016 at 04:37:24 UTC, Øivind wrote: Should I file a ticket for this? It is already known, just nobody has fixed it yet (and probably won't for a long time still)

Re: static init of associative array of enums not working.. why?

2016-02-26 Thread Øivind via Digitalmars-d-learn
On Saturday, 27 February 2016 at 04:35:41 UTC, Adam D. Ruppe wrote: It just isn't implemented in the compiler. Instead, you can declare it outside and set it in a static module constructor: That was quick! Thank you. Should I file a ticket for this?

Re: static init of associative array of enums not working.. why?

2016-02-26 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 27 February 2016 at 04:15:06 UTC, Øivind wrote: Shouldn't this work? According to "Static Initialization of AAs" on this page, it should: https://dlang.org/spec/hash-map.html It just isn't implemented in the compiler. Instead, you can declare it outside and set it in a static mod

static init of associative array of enums not working.. why?

2016-02-26 Thread Øivind via Digitalmars-d-learn
Shouldn't this work? According to "Static Initialization of AAs" on this page, it should: https://dlang.org/spec/hash-map.html enum DevicePropDataType { dString, dDateTime } enum DevicePropValType { property, miscDate } immutable DevicePropDataType[DevicePropValType] propDType = [

Re: Using enums as function parameters (in a minimized way)

2015-12-01 Thread tcak via Digitalmars-d-learn
On Tuesday, 1 December 2015 at 10:50:04 UTC, Rikki Cattermole wrote: On 01/12/15 11:44 PM, Ozan wrote: Hi Let's say we have an enum like enum SomethingAboutChristmas { SantaClaus, Angel, Tree } and want to use it in a function like void goingChristmas(SomethingAboutChristmas enum

Re: Using enums as function parameters (in a minimized way)

2015-12-01 Thread tcak via Digitalmars-d-learn
On Tuesday, 1 December 2015 at 13:03:37 UTC, tcak wrote: On Tuesday, 1 December 2015 at 10:50:04 UTC, Rikki Cattermole wrote: [...] This is like: Q) I want to write an OS. How? A) Write in assembly. What Ozan says is logical. Compiler should assume it in that way normally. I have thorough

Re: Using enums as function parameters (in a minimized way)

2015-12-01 Thread Daniel Kozak via Digitalmars-d-learn
V Tue, 01 Dec 2015 10:44:06 + Ozan via Digitalmars-d-learn napsáno: > Hi > > Let's say we have an enum like > > enum SomethingAboutChristmas { >SantaClaus, >Angel, >Tree > } > > and want to use it in a function like > >void goingChristmas(SomethingAboutChristmas enumvalue)

Re: Using enums as function parameters (in a minimized way)

2015-12-01 Thread Rikki Cattermole via Digitalmars-d-learn
On 01/12/15 11:44 PM, Ozan wrote: Hi Let's say we have an enum like enum SomethingAboutChristmas { SantaClaus, Angel, Tree } and want to use it in a function like void goingChristmas(SomethingAboutChristmas enumvalue) it works fine like following goingChristmas(SomethingAbout

Using enums as function parameters (in a minimized way)

2015-12-01 Thread Ozan via Digitalmars-d-learn
Hi Let's say we have an enum like enum SomethingAboutChristmas { SantaClaus, Angel, Tree } and want to use it in a function like void goingChristmas(SomethingAboutChristmas enumvalue) it works fine like following goingChristmas(SomethingAboutChristmas.Tree) I prefer to use a short

Re: Classes as enums in D?

2015-11-30 Thread Andrew LaChance via Digitalmars-d-learn
On Monday, 30 November 2015 at 08:08:20 UTC, Meta wrote: This doesn't quite work in D; you'd have to make each WhiteKey const (which is probably not a bad idea anyway if you're using it like an enum). However, it's better to just do this with plain old value-type structs. It's exactly the same

Re: Classes as enums in D?

2015-11-30 Thread Meta via Digitalmars-d-learn
utput: 470910 470910 You're print the address of `f` and `n` on the stack, not the reference they're pointing to. But it's true that enums of mutable _arrays_ do create a new instance every time they're used: enum X { A = [1,2,3], B = [4,5,6], } voi

Re: Classes as enums in D?

2015-11-30 Thread Marc Schütz via Digitalmars-d-learn
u're print the address of `f` and `n` on the stack, not the reference they're pointing to. But it's true that enums of mutable _arrays_ do create a new instance every time they're used: enum X { A = [1,2,3], B = [4,5,6], } void main() {

Re: Classes as enums in D?

2015-11-30 Thread Meta via Digitalmars-d-learn
oject I'm working on, porting it from java to D. One of the uncommonly-used features of java that I like is how enums can be full classes (though I don't like that there's no option to use enums as e.g. regular ints). This allows several benefits, such as the ability to use them

Re: Classes as enums in D?

2015-11-30 Thread Rikki Cattermole via Digitalmars-d-learn
On 30/11/15 8:58 PM, Andrew LaChance wrote: On Monday, 30 November 2015 at 07:54:49 UTC, Rikki Cattermole wrote: enums don't have to be integral, but for performance reasons it is for the best. enum Foo : string { A = "a", B = "b", C = "d",

Re: Classes as enums in D?

2015-11-30 Thread Mike Parker via Digitalmars-d-learn
On Monday, 30 November 2015 at 07:58:43 UTC, Andrew LaChance wrote: Oh interesting. So you are saying I could have a struct WhiteKey {...} and then an enum that extends WhiteKey? enums can't *extend* anything. You can do this: struct WhiteKeyS { immutable int halfStepsToPre

Re: Classes as enums in D?

2015-11-30 Thread Andrew LaChance via Digitalmars-d-learn
On Monday, 30 November 2015 at 07:54:49 UTC, Rikki Cattermole wrote: enums don't have to be integral, but for performance reasons it is for the best. enum Foo : string { A = "a", B = "b", C = "d", ERROR = "What are you talking about?&qu

Re: Classes as enums in D?

2015-11-29 Thread Rikki Cattermole via Digitalmars-d-learn
porting it from java to D. One of the uncommonly-used features of java that I like is how enums can be full classes (though I don't like that there's no option to use enums as e.g. regular ints). This allows several benefits, such as the ability to use them in switch statements like reg

Classes as enums in D?

2015-11-29 Thread Andrew LaChance via Digitalmars-d-learn
commonly-used features of java that I like is how enums can be full classes (though I don't like that there's no option to use enums as e.g. regular ints). This allows several benefits, such as the ability to use them in switch statements like regular enums, the full set of obj

Re: Packing enums

2015-07-01 Thread ketmar via Digitalmars-d-learn
{ foo, bar, baz }; align(1) struct TwoEnums(E0, E1) if (is(E0 == enum) && is(E1 == enum)) { private import std.string : format; static assert(E0.min >= 0 && E1.min >= 0 && E0.max < 256 && E1.max < 256 && E0.max+E1.max < 256, "enu

Packing enums

2015-06-30 Thread qznc via Digitalmars-d-learn
I stumbled upon this interesting programming challenge [0], which imho should be possible to implement in D. Maybe someone here wants to try. Task: Given two enums with less than 256 states, pack them into one byte and provide convenient accessor functions. Something like this: enum X { A

Re: Packing enums

2015-06-30 Thread Nicholas Wilson via Digitalmars-d-learn
On Monday, 29 June 2015 at 22:05:47 UTC, qznc wrote: I stumbled upon this interesting programming challenge [0], which imho should be possible to implement in D. Maybe someone here wants to try. Task: Given two enums with less than 256 states, pack them into one byte and provide convenient

Re: CTFE & enums & static assert

2015-05-18 Thread Robert M. Münch via Digitalmars-d-learn
On 2015-05-15 17:26:50 +, Ali Çehreli said: On 05/15/2015 09:45 AM, Robert M. Münch wrote: > Is there a way I can build an ENUM from within the FOREACH? What I want > to achive is, that I would like to use: > > final switch (myEnum) ... Sorry, I don't understand your question. :( Do y

Re: CTFE & enums & static assert

2015-05-15 Thread Ali Çehreli via Digitalmars-d-learn
On 05/15/2015 09:45 AM, Robert M. Münch wrote: > On 2015-05-04 18:22:17 +, Ali Çehreli said: >> TypeTuple is great because it enables "compile-time foreach" >> (unfortunately, implicitly): >> >> foreach (m; __traits(allMembers, A)) { >> // This body is repeated for each member

Re: CTFE & enums & static assert

2015-05-15 Thread Robert M. Münch via Digitalmars-d-learn
On 2015-05-04 18:22:17 +, Ali Çehreli said: There are many different kinds of tuples in D, only two of which I can handle: 1) Tuple from std.typecons, which are ordinarily created at run time 2) TypeTuple from std.typetuple, which are compile-time entities The documentation is not clear t

Re: CTFE & enums & static assert

2015-05-05 Thread Robert M. Münch via Digitalmars-d-learn
On 2015-05-04 22:22:51 +, ketmar said: as i said, `typeid` is runtime feature, so you can't print it with pragma. and tuples aren't exist in runtime, it's compile-time only. i think you are even more confused now. ;-) sorry. No, that makes it much more clearer for me. The compiler should

Re: CTFE & enums & static assert

2015-05-04 Thread ketmar via Digitalmars-d-learn
On Mon, 04 May 2015 20:07:27 +0200, Robert M. Münch wrote: > > Gives this: > > (string, string, string) > playground.d(9): Error: no type for typeid(members1) > playground.d(9):while evaluating pragma(msg, typeid(members1)) `typeid` is runtime thing, you can't use it in compile-time.

Re: CTFE & enums & static assert

2015-05-04 Thread Ali Çehreli via Digitalmars-d-learn
On 05/04/2015 11:07 AM, Robert M. Münch wrote: > enum A {a, b, c}; > > enum members1 = __traits(allMembers, A); > auto members2 = __traits(allMembers, A); > > 1. So the (string, string, string) output is based on the expression of > members1? Meaning, the right-hand-side. There are many differe

Re: CTFE & enums & static assert

2015-05-04 Thread Robert M. Münch via Digitalmars-d-learn
constants", and that constants has the corresponding type, they aren't enums. Hmm... Ok, I understand that these seems to be two different things. Not sure if I now understand this correct then: enum A {a, b, c}; enum members1 = __traits(allMembers, A); auto members2 = __traits(allMe

Re: CTFE & enums & static assert

2015-05-04 Thread ketmar via Digitalmars-d-learn
nstructed. that's due to `enum` keyword abusing. "enum type" is something like this: enum MyEnum { A, B } and enum val = false; is a compile-time boolean constant, which will be inlined on using. i.e. compiler will inline such "enum constants", and that constants has the corresponding type, they aren't enums. signature.asc Description: PGP signature

CTFE & enums & static assert

2015-05-04 Thread Robert M. Münch via Digitalmars-d-learn
I find this a bit strange: // get all rules that start with p... enum BolRules = StaticFilter!(beginsWithP, __traits(allMembers,BolSource)); static assert(is(BolRules == enum)); Compiling using dmd... source/app.d(114): Error: static assert (is(BolRules == enum)) is false I'm trying to constru

Re: Tagged enums why reserved words are not permitted ?

2014-10-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 28, 2014 16:58:50 Gary Willoughby via Digitalmars-d-learn wrote: > On Tuesday, 28 October 2014 at 00:51:17 UTC, Jonathan M Davis via > Digitalmars-d-learn > > > The > > thing that's been done in Phobos in this type of situation is > > to put an > > underscore on the end of the

Re: Tagged enums why reserved words are not permitted ?

2014-10-28 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 28 October 2014 at 00:51:17 UTC, Jonathan M Davis via Digitalmars-d-learn The thing that's been done in Phobos in this type of situation is to put an underscore on the end of the keyword, so you'd get enum CrudOps { read, write, delete_ } and while that may not be what you want, i

Re: Tagged enums why reserved words are not permitted ?

2014-10-28 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/27/14 8:31 PM, Domingo wrote: Hello ! I'm not sure if I'm missing something here but for a tagged enum it doesn't seem to make sense to forbid reserved keywords like: enum CrudOps {read, write, delete} The dmd compiler are complaining: -- cte.d(4): Error: basic type expected, not del

Re: Tagged enums why reserved words are not permitted ?

2014-10-28 Thread Jacob Carlborg via Digitalmars-d-learn
On 2014-10-28 01:51, Jonathan M Davis via Digitalmars-d-learn wrote: And I've never seen a language where it did (though one may exist out there somewhere) Ruby: class Foo end Foo == Foo.new.class # perfectly legal You always need to have a receiver when calling the "class" method. This i

Re: Tagged enums why reserved words are not permitted ?

2014-10-27 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Oct 28, 2014 at 12:31:43AM +, Domingo via Digitalmars-d-learn wrote: > Hello ! > > I'm not sure if I'm missing something here but for a tagged enum it > doesn't seem to make sense to forbid reserved keywords like: > > enum CrudOps {read, write, delete} > > The dmd compiler are compla

Re: Tagged enums why reserved words are not permitted ?

2014-10-27 Thread ketmar via Digitalmars-d-learn
On Tue, 28 Oct 2014 00:31:43 + Domingo via Digitalmars-d-learn wrote: > Hello ! > > I'm not sure if I'm missing something here but for a tagged enum > it doesn't seem to make sense to forbid reserved keywords like: > > enum CrudOps {read, write, delete} > > The dmd compiler are complainin

Re: Tagged enums why reserved words are not permitted ?

2014-10-27 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 28, 2014 00:31:43 Domingo via Digitalmars-d-learn wrote: > Hello ! > > I'm not sure if I'm missing something here but for a tagged enum > it doesn't seem to make sense to forbid reserved keywords like: > > enum CrudOps {read, write, delete} > > The dmd compiler are complaining:

Tagged enums why reserved words are not permitted ?

2014-10-27 Thread Domingo via Digitalmars-d-learn
Hello ! I'm not sure if I'm missing something here but for a tagged enum it doesn't seem to make sense to forbid reserved keywords like: enum CrudOps {read, write, delete} The dmd compiler are complaining: -- cte.d(4): Error: basic type expected, not delete cte.d(4): Error: no identifier

Re: enums

2014-06-01 Thread Chris Nicholson-Sauls via Digitalmars-d-learn
On Saturday, 31 May 2014 at 22:45:32 UTC, bearophile wrote: Chris Nicholson-Sauls: Good... I was starting to fear I was the only one. In general you can't fix the names in a language because you always find someone that likes the ones present :) I think "enum" is a bad name for the purpose

Re: enums

2014-06-01 Thread Philippe Sigaud via Digitalmars-d-learn
On Sat, May 31, 2014 at 10:14 PM, bearophile via Digitalmars-d-learn wrote: >> In contrast to those two examples where immutable can be used at compile >> time, what are some other cases where it is necessary to use enum instead >> of immutable? > > > By default use enum if you define a compile-t

Re: enums

2014-05-31 Thread bearophile via Digitalmars-d-learn
Chris Nicholson-Sauls: Good... I was starting to fear I was the only one. In general you can't fix the names in a language because you always find someone that likes the ones present :) I think "enum" is a bad name for the purpose of defining manifest constants, but I don't think this will

Re: enums

2014-05-31 Thread Chris Nicholson-Sauls via Digitalmars-d-learn
On Saturday, 31 May 2014 at 22:13:35 UTC, monarch_dodra wrote: On Saturday, 31 May 2014 at 21:21:59 UTC, Paul D Anderson wrote: 'enum' as a manifest constant keyword has been an unpopular decision from its introduction. "Everybody" agrees that it should be changed. Everybody but Walter I find

Re: enums

2014-05-31 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 31 May 2014 at 21:21:59 UTC, Paul D Anderson wrote: 'enum' as a manifest constant keyword has been an unpopular decision from its introduction. "Everybody" agrees that it should be changed. Everybody but Walter I find enum makes sense.

Re: enums

2014-05-31 Thread Timon Gehr via Digitalmars-d-learn
On 05/31/2014 11:21 PM, Paul D Anderson wrote: On Saturday, 31 May 2014 at 20:14:59 UTC, bearophile wrote: Miles Stoudenmire: In contrast to those two examples where immutable can be used at compile time, what are some other cases where it is necessary to use enum instead of immutable? By de

Re: enums

2014-05-31 Thread bearophile via Digitalmars-d-learn
Paul D Anderson: 'enum' as a manifest constant keyword has been an unpopular decision from its introduction. I agree, I too asked for a better name. Bye, bearophile

Re: enums

2014-05-31 Thread Paul D Anderson via Digitalmars-d-learn
On Saturday, 31 May 2014 at 20:14:59 UTC, bearophile wrote: Miles Stoudenmire: In contrast to those two examples where immutable can be used at compile time, what are some other cases where it is necessary to use enum instead of immutable? By default use enum if you define a compile-time-kno

Re: enums

2014-05-31 Thread bearophile via Digitalmars-d-learn
Miles Stoudenmire: In contrast to those two examples where immutable can be used at compile time, what are some other cases where it is necessary to use enum instead of immutable? By default use enum if you define a compile-time-known value, unless it's composed data like an array, etc. By

Re: enums

2014-05-31 Thread Miles Stoudenmire via Digitalmars-d-learn
te: > > On Friday, 30 May 2014 at 15:30:15 UTC, Russel Winder via > Digitalmars-d-learn wrote: > >> > >> I think I have no idea what D enums are about. > >> > >> Bearophile's example of some code in an email on another thread

Re: enums

2014-05-31 Thread Andrej Mitrovic via Digitalmars-d-learn
On Friday, 30 May 2014 at 15:30:15 UTC, Russel Winder via Digitalmars-d-learn wrote: >> >> I think I have no idea what D enums are about. >> >> Bearophile's example of some code in an email on another thread uses: >> >> enum double p0 = 0.0045; >&

Re: enums

2014-05-30 Thread via Digitalmars-d-learn
Some explanation how the seemingly different concepts "enumerated value" and "manifest constant" are actually related: Let's start with the "classical" enums as they're known from C/C++. They are used to create lists of symbolic names: enum Color {

Re: enums

2014-05-30 Thread Philippe Sigaud via Digitalmars-d-learn
On Fri, May 30, 2014 at 7:56 PM, Steven Schveighoffer wrote: > You can as long as the value is known at compile time: > > http://dpaste.dzfl.pl/5a710bd80ab0 Oh wow. And that works for static if also: http://dpaste.dzfl.pl/f87321a47834 Man. That opens some new possibilities.

Re: enums

2014-05-30 Thread Steven Schveighoffer via Digitalmars-d-learn
constant. Interestingly, it can be thought of like a C macro, being pasted inside source code. Avoid enums for arrays and associative arrays as they are hidden performance sinks. The constant gets regenerated at runtime every time it is used in an expression. I guess that, in an ideal world

Re: enums

2014-05-30 Thread Philippe Sigaud via Digitalmars-d-learn
, being pasted inside source code. > > Avoid enums for arrays and associative arrays as they are hidden performance > sinks. The constant gets regenerated at runtime every time it is used in an > expression. I guess that, in an ideal world, immutable variables could be use at compile-t

  1   2   3   >