Thread overview
Auto attributes for functions
Aug 20, 2014
uri
Aug 20, 2014
Meta
Aug 20, 2014
Jonathan M Davis
Aug 20, 2014
ed
August 20, 2014
Hi all,

Bit new to D so this might be a very naive question...

Can the compiler auto infer function attributes?

I am often adding as many attributes as possible and use the compiler to show me where they're not applicable and take them away. It would be great if this could be achieved like so:

auto function() @auto
{}

instead of manually writing:

auto function() pure @safe nothrow @nogc const
{}

cheers,
uri





August 20, 2014
On Wednesday, 20 August 2014 at 01:38:53 UTC, uri wrote:
> Hi all,
>
> Bit new to D so this might be a very naive question...
>
> Can the compiler auto infer function attributes?
>
> I am often adding as many attributes as possible and use the compiler to show me where they're not applicable and take them away. It would be great if this could be achieved like so:
>
> auto function() @auto
> {}
>
> instead of manually writing:
>
> auto function() pure @safe nothrow @nogc const
> {}
>
> cheers,
> uri

Only if they're template functions.

//inferred as @safe pure nothrow @nogc
auto fun()() {}

//Compiler treats this is @system impure throwing @gc
//(if the latter three existed)
auto fun() {}

I think Andrei suggested in the past that functions with a return type of "auto" have attributes inferred for them as well, but some people were against it, and nobody's tried implementing it yet.
August 20, 2014
On Wed, 20 Aug 2014 01:38:52 +0000
uri via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:

> Hi all,
>
> Bit new to D so this might be a very naive question...
>
> Can the compiler auto infer function attributes?
>
> I am often adding as many attributes as possible and use the compiler to show me where they're not applicable and take them away. It would be great if this could be achieved like so:
>
> auto function() @auto
> {}
>
> instead of manually writing:
>
> auto function() pure @safe nothrow @nogc const
> {}

Currently, just templated functions get their attributes inferred. The biggest problem with inferring them for all functions is that you can declare a function without defining it in the same place (e.g. if you're using .di files), in which case the compiler has no function body to use for attribute inferrence.

There have been discussions on ways to reasonably infer attributes under more circumstances, but nothing has come of them yet. However, I'd expect that there will be at least some improvements to the situation at some point given that there is a general consensus that while the attributes are quite useful, it's also rather annoying to have to keep typing them all.

- Jonathan M Davis
August 20, 2014
On Wednesday, 20 August 2014 at 09:13:15 UTC, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Wed, 20 Aug 2014 01:38:52 +0000
> uri via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>
>> Hi all,
>>
>> Bit new to D so this might be a very naive question...
>>
>> Can the compiler auto infer function attributes?
>>
>> I am often adding as many attributes as possible and use the
>> compiler to show me where they're not applicable and take them
>> away. It would be great if this could be achieved like so:
>>
>> auto function() @auto
>> {}
>>
>> instead of manually writing:
>>
>> auto function() pure @safe nothrow @nogc const
>> {}
>
> Currently, just templated functions get their attributes inferred. The biggest
> problem with inferring them for all functions is that you can declare a
> function without defining it in the same place (e.g. if you're using .di
> files), in which case the compiler has no function body to use for attribute
> inferrence.
>
> There have been discussions on ways to reasonably infer attributes under more
> circumstances, but nothing has come of them yet. However, I'd expect that
> there will be at least some improvements to the situation at some point given
> that there is a general consensus that while the attributes are quite useful,
> it's also rather annoying to have to keep typing them all.
>
> - Jonathan M Davis

Thanks guys for the info.

/uri