www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Auto attributes for functions

reply "uri" <email ether.com> writes:
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
Aug 19 2014
next sibling parent "Meta" <jared771 gmail.com> writes:
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.
Aug 19 2014
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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
Aug 20 2014
parent "ed" <gmail gmail.com> writes:
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
Aug 20 2014