December 15, 2013
On Sunday, 15 December 2013 at 12:40:53 UTC, monarch_dodra wrote:
> On Sunday, 15 December 2013 at 09:38:28 UTC, deadalnix wrote:
>> Resulting in people giving name like TestT1, TestT2 as enum values in C++. As a result, you end up with the same verbosity as in D, without the possibility of using 'with'.
>
> I've usually seen the "namespace" or "struct "approach, eg:
>
> namespace CheckerBoardColor
> // or struct CheckerBoardColor
> {
>     enum Enumeration
>     {
>         Red,
>         Black,
>     };
> };
>
> This allows using "CheckerBoardColor::Red", which (IMO) is nice and verbose. you can use "using CheckerBoardColor" for the equivalent of "with" (namespace only).
>
> Unfortunatly, the actual enum "type" is "CheckerBoardColor::Enumeration", which is strangely verbose.

I'd rather do this:

namespace CheckerBoardColorNamespace
{
    enum CheckerBoardColor { Red, Black };
};
using CheckerBoardColorNamespace::CheckerBoardColor;

auto v = CheckerBoardColor::Red;

int main()
{
    using namespace CheckerBoardColorNamespace;
    auto v = Red;
}

...and you get to have a nice name for the enum type.
December 15, 2013
On Sunday, 15 December 2013 at 11:27:51 UTC, bearophile wrote:
> And some people have criticized the verbosity in special situations, like this:
>
> enum Foo { good, bad }
> void bar(Foo f) {}
> void main() {
>     // bar(bad); // Not enough
>     bar(Foo.bad);
> }

A related discussion:
http://forum.dlang.org/thread/lssmuukdltmooehlnfpf@forum.dlang.org
December 15, 2013
On Sunday, 15 December 2013 at 15:01:47 UTC, Tommi wrote:
>
> I'd rather do this:
>
> namespace CheckerBoardColorNamespace
> {
>     enum CheckerBoardColor { Red, Black };
> };
> using CheckerBoardColorNamespace::CheckerBoardColor;
>
> auto v = CheckerBoardColor::Red;
>
> int main()
> {
>     using namespace CheckerBoardColorNamespace;
>     auto v = Red;
> }
>
> ...and you get to have a nice name for the enum type.

That's a C++11 extension. "CheckerBoardColor::Red" is not a legal
value. There are no scoped enums in C++98/03.

That said, nice trick for "using namespace
CheckerBoardColorNamespace;" It's a nice way to emulate "with".
Too bad this means the enum (AFAIK) can't be strongly C++11-stlye
typed.

BTH though, if I had C++11 in my workplace, I'd just class it and
move on.
December 15, 2013
On Sunday, 15 December 2013 at 11:27:51 UTC, bearophile wrote:
> By the way, I am not criticizing "D verbose approach". I have criticized the weak typing:
>
>
> enum Foo { good, bad }
> void main() {
>     int x = Foo.good; // Weak typing.
> }
>

This is not a weak typing. An example of weak typing is array literal [].
There is nothing wrong with converting enum to base type, since enum is always subset of base type. Explicit cast in such case is useless. What is your point against current behavior?

> And some people have criticized the verbosity in special situations, like this:
>
>
> enum Foo { good, bad }
> void bar(Foo f) {}
> void main() {
>     // bar(bad); // Not enough
>     bar(Foo.bad);
> }
>
> Bye,
> bearophile

because 'bad' is undefined identifier, but I see your point.
1 2
Next ›   Last »