What is that "enum function" supposed to be ?

Elmar chrehme at gmx.de
Thu Jul 22 13:31:38 UTC 2021


On Monday, 19 July 2021 at 21:31:40 UTC, Paul Backus wrote:
> On Monday, 19 July 2021 at 21:16:57 UTC, Elmar wrote:
>> I'd like to know:
>> - What does return type `enum` mean?
>
> It just means `auto`. Probably it shouldn't be allowed at all, 
> but D's parser is sometimes overly-permissive and will accept 
> things like this even when they don't really make sense.
>
> For example, you can also declare a `@nogc` variable:
>
> ```d
> @nogc x = new Object;
> ```
>
> The `@nogc` attribute doesn't do anything here, but the 
> compiler allows it anyway.

Thank you all for your answers. I didn't know you can skip `auto` 
by using an attribute :-o but I had hoped this would be possible.

Just wait for some time ;-) . I'm going to make this piece of 
code work, but using a different syntax:

```d
auto x = GC!Object(arg1, arg2);  // allocates using D's GC
auto y = GC!Object.init;         // zero-arg constructor call
@gc a  = new Object;             // @gc represents a "memory 
contract" assumption
auto z = GC!Object.init!a;       // constructor call with 
compile-time argument, checks memory contract (statically or 
dynamically)
```

I've started work on a simple memory contract library which 
should allow people to replace the old `new` keyword with a 
struct name like `GC`.

I hope it will make using alternative allocators very simple and 
will make allocations and memory requirements finally transparent 
for API users.


More information about the Digitalmars-d mailing list