www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - safe, pure and nothrow at the beginning of a module

reply "Philippe Sigaud" <philippe.sigaud gmail.com> writes:
So I'm trying to use  safe, pure and nothrow.

If I understand correctly Adam Ruppe's Cookbook, by putting

 safe:
pure:
nothrow:

at the beginning of a module, I distribute it on all definitions, 
right? Even methods, inner classes, and so on?

Because I did just that on half a dozen of modules and the 
compiler did not complain. Does that mean my code is clean(?) or 
that what I did has no effect?
Aug 15 2014
next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, 15 August 2014 at 16:54:54 UTC, Philippe Sigaud wrote:
 So I'm trying to use  safe, pure and nothrow.

 If I understand correctly Adam Ruppe's Cookbook, by putting

  safe:
 pure:
 nothrow:

 at the beginning of a module, I distribute it on all 
 definitions, right? Even methods, inner classes, and so on?

 Because I did just that on half a dozen of modules and the 
 compiler did not complain. Does that mean my code is clean(?) 
 or that what I did has no effect?
Hmmm... It _should_ apply to everything, but maybe it only applies to the outer-level declarations. Certainly, in most cases, I'd be surprised if marking everything in a module with those attributes would work on the first go. It's _possible_, depending on what you're doing, but in my experience, odds are that you're doing _something_ that violates one or all of those in several places. - Jonathan M Davis
Aug 15 2014
parent Philippe Sigaud via Digitalmars-d-learn writes:
In another module I marked as ' safe:' at the top, the compiler told
me that a class opEquals could not be  safe (because Object.opEquals
is  system).

So it seems that indeed a module-level ' safe:' affects everything,
since a class method was found lacking.

(I put a  trusted attribute on it).
Aug 15 2014
prev sibling parent reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
On Friday, 15 August 2014 at 16:54:54 UTC, Philippe Sigaud wrote:
 So I'm trying to use  safe, pure and nothrow.

 If I understand correctly Adam Ruppe's Cookbook, by putting

  safe:
 pure:
 nothrow:

 at the beginning of a module, I distribute it on all 
 definitions, right? Even methods, inner classes, and so on?

 Because I did just that on half a dozen of modules and the 
 compiler did not complain. Does that mean my code is clean(?) 
 or that what I did has no effect?
I've noticed the same thing. If I want pure and nothrow to propage to inner structs and classes I have to place another label inside the class definition. Otherwise only free functions are affected.
Aug 15 2014
parent reply "Philpax" <phillip philliplarkson.com> writes:
On Friday, 15 August 2014 at 23:22:27 UTC, Vlad Levenfeld wrote:
 On Friday, 15 August 2014 at 16:54:54 UTC, Philippe Sigaud 
 wrote:
 So I'm trying to use  safe, pure and nothrow.

 If I understand correctly Adam Ruppe's Cookbook, by putting

  safe:
 pure:
 nothrow:

 at the beginning of a module, I distribute it on all 
 definitions, right? Even methods, inner classes, and so on?

 Because I did just that on half a dozen of modules and the 
 compiler did not complain. Does that mean my code is clean(?) 
 or that what I did has no effect?
I've noticed the same thing. If I want pure and nothrow to propage to inner structs and classes I have to place another label inside the class definition. Otherwise only free functions are affected.
I had a similar experience when trying to use nogc. Having to insert nogc into every struct I use is mildly annoying.
Aug 15 2014
next sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
 If I understand correctly Adam Ruppe's Cookbook, by putting

  safe:
 pure:
 nothrow:

 at the beginning of a module, I distribute it on all definitions, right?
 Even methods, inner classes, and so on?
I read Adam's book again and I was wrong: Chapter 7, p. 173: "You may add safe: to the top of your module and aggregate definitions to apply the annotation to all function that follows, instead of writing it on each individual function." So, first, he's talking only about safe (though I suppose it works the same for all annotations) and he says: *and aggregate definitions*. We indeed need to put annotations inside aggregates to affect their innards. If that's true, I have a lot of annotation sprinkling to do.
Aug 16 2014
prev sibling next sibling parent Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On 08/16/14 13:18, Philippe Sigaud via Digitalmars-d-learn wrote:
 We indeed need to put annotations inside aggregates to
 affect their innards.
 
 If that's true, I have a lot of annotation sprinkling to do.
It's not true for safe, but true for some other attributes. http://forum.dlang.org/post/mailman.125.1397731134.2763.digitalmars-d puremagic.com artur
Aug 16 2014
prev sibling next sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
On Sat, Aug 16, 2014 at 1:30 PM, Artur Skawina via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 On 08/16/14 13:18, Philippe Sigaud via Digitalmars-d-learn wrote:
 We indeed need to put annotations inside aggregates to
 affect their innards.

 If that's true, I have a lot of annotation sprinkling to do.
It's not true for safe, but true for some other attributes. http://forum.dlang.org/post/mailman.125.1397731134.2763.digitalmars-d puremagic.com
Okay... So safe includes child scopes. I suppose trusted and system work in the same way. *but* nothrow, nogc and UDA's do not include child scopes. Putting them at the beginning of a module will not affect methods in aggregates... What's the situation for pure? (I don't have a D compiler handy right now, or I would test it myself).
Aug 16 2014
prev sibling next sibling parent Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On 08/16/14 13:58, Philippe Sigaud via Digitalmars-d-learn wrote:
 On Sat, Aug 16, 2014 at 1:30 PM, Artur Skawina via Digitalmars-d-learn
 http://forum.dlang.org/post/mailman.125.1397731134.2763.digitalmars-d puremagic.com
Okay... So safe includes child scopes. I suppose trusted and system work in the same way. *but* nothrow, nogc and UDA's do not include child scopes. Putting them at the beginning of a module will not affect methods in aggregates... What's the situation for pure? (I don't have a D compiler handy right now, or I would test it myself).
safe, trusted, system, shared, immutable, const, inout and `extern (...)` affect child scopes. `synchronized` does too, but in a rather unintuitive way; hopefully nobody uses this. ;) Other attributes, including 'pure' and 'nothrow' only affect symbols in the current scope. artur
Aug 16 2014
prev sibling next sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
Artur:
  safe,  trusted,  system, shared, immutable, const, inout and `extern (...)`
 affect child scopes. `synchronized` does too, but in a rather unintuitive
 way; hopefully nobody uses this. ;)
Well, I also hope no one uses inout: at the module level?
 Other attributes, including 'pure' and 'nothrow' only affect symbols
 in the current scope.
There we are. Good to know, thanks.
Aug 16 2014
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Sat, 16 Aug 2014 14:39:00 +0200
Artur Skawina via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On 08/16/14 13:58, Philippe Sigaud via Digitalmars-d-learn wrote:
 On Sat, Aug 16, 2014 at 1:30 PM, Artur Skawina via
 Digitalmars-d-learn
 http://forum.dlang.org/post/mailman.125.1397731134.2763.digitalmars-d puremagic.com
Okay... So safe includes child scopes. I suppose trusted and system work in the same way. *but* nothrow, nogc and UDA's do not include child scopes. Putting them at the beginning of a module will not affect methods in aggregates... What's the situation for pure? (I don't have a D compiler handy right now, or I would test it myself).
safe, trusted, system, shared, immutable, const, inout and `extern (...)` affect child scopes. `synchronized` does too, but in a rather unintuitive way; hopefully nobody uses this. ;) Other attributes, including 'pure' and 'nothrow' only affect symbols in the current scope.
It sounds like a bug to me if they're not consistent. - Jonathan M Davis
Aug 16 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 16 August 2014 at 19:30:16 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:
 On Sat, 16 Aug 2014 14:39:00 +0200
 Artur Skawina via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 On 08/16/14 13:58, Philippe Sigaud via Digitalmars-d-learn 
 wrote:
 On Sat, Aug 16, 2014 at 1:30 PM, Artur Skawina via
 Digitalmars-d-learn
 http://forum.dlang.org/post/mailman.125.1397731134.2763.digitalmars-d puremagic.com
Okay... So safe includes child scopes. I suppose trusted and system work in the same way. *but* nothrow, nogc and UDA's do not include child scopes. Putting them at the beginning of a module will not affect methods in aggregates... What's the situation for pure? (I don't have a D compiler handy right now, or I would test it myself).
safe, trusted, system, shared, immutable, const, inout and `extern (...)` affect child scopes. `synchronized` does too, but in a rather unintuitive way; hopefully nobody uses this. ;) Other attributes, including 'pure' and 'nothrow' only affect symbols in the current scope.
It sounds like a bug to me if they're not consistent. - Jonathan M Davis
Well, you got system to override safe, but no impure or throws. So the behavior can kind of make sense in a way. Maybe.
Aug 16 2014
next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Saturday, 16 August 2014 at 20:48:25 UTC, monarch_dodra wrote:
 On Saturday, 16 August 2014 at 19:30:16 UTC, Jonathan M Davis 
 via Digitalmars-d-learn wrote:
 On Sat, 16 Aug 2014 14:39:00 +0200
 Artur Skawina via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:
  safe,  trusted,  system, shared, immutable, const, inout and 
 `extern
 (...)` affect child scopes. `synchronized` does too, but in a 
 rather
 unintuitive way; hopefully nobody uses this. ;)

 Other attributes, including 'pure' and 'nothrow' only affect 
 symbols
 in the current scope.
It sounds like a bug to me if they're not consistent. - Jonathan M Davis
Well, you got system to override safe, but no impure or throws. So the behavior can kind of make sense in a way. Maybe.
Except that attributes like const, immutable, shared, and inout can't be reversed either (in fact system, trusted, and safe - and maybe extern - are the only ones from that list that can be, so while I could see making that separation, that's not what's actually happening. - Jonathan M Davis
Aug 16 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 16 Aug 2014 20:48:23 +0000
monarch_dodra via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 Well, you got   system to override  safe, but no  impure or=20
  throws. So the behavior can kind of make sense in a way. Maybe.
talking about attributes. the unability to reverse 'final:' makes me mad. i'm almost ready to patch the compiler myself and start using this feature even if it will be never accepted in mainline, like i'm doing now with 'foreach (auto i; ...)'.
Aug 16 2014