www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Check whether function/delegate uses any shared or global variables

reply Nikhil Jacob <nikhiljaco gmail.com> writes:
Is there any way to check whether a function/delegate passed to a 
function uses any shared or global variables ?

I could not find any in std.traits.
Dec 12 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 12 December 2016 at 11:02:21 UTC, Nikhil Jacob wrote:
 Is there any way to check whether a function/delegate passed to 
 a function uses any shared or global variables ?

 I could not find any in std.traits.
there is the pure function attribute, how ever this still allows you to use globals *if you pass them as parameters to the function*. see https://dlang.org/spec/function.html#pure-functions
Dec 12 2016
next sibling parent reply Nikhil Jacob <nikhiljaco gmail.com> writes:
On Monday, 12 December 2016 at 11:15:28 UTC, Nicholas Wilson 
wrote:
 On Monday, 12 December 2016 at 11:02:21 UTC, Nikhil Jacob wrote:
 Is there any way to check whether a function/delegate passed 
 to a function uses any shared or global variables ?

 I could not find any in std.traits.
there is the pure function attribute, how ever this still allows you to use globals *if you pass them as parameters to the function*. see https://dlang.org/spec/function.html#pure-functions
Make sense.. I have two follow up questions 1. What about delegates ? 2. If a function is not explicitly declared as pure but satisfies the conditions of a pure function then can i check whether the function is pure using functionAttributes in std.traits.
Dec 12 2016
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 12 December 2016 at 11:37:04 UTC, Nikhil Jacob wrote:
 On Monday, 12 December 2016 at 11:15:28 UTC, Nicholas Wilson 
 wrote:
 On Monday, 12 December 2016 at 11:02:21 UTC, Nikhil Jacob 
 wrote:
 Is there any way to check whether a function/delegate passed 
 to a function uses any shared or global variables ?

 I could not find any in std.traits.
there is the pure function attribute, how ever this still allows you to use globals *if you pass them as parameters to the function*. see https://dlang.org/spec/function.html#pure-functions
Make sense.. I have two follow up questions 1. What about delegates ? 2. If a function is not explicitly declared as pure but satisfies the conditions of a pure function then can i check whether the function is pure using functionAttributes in std.traits.
1. they work the same. Delegates can have any of the function attributes that a function can, as well as scope, meaning the delegate will not be stored. 2. Yes and no. Template functions have their attributes inferred (but you can still annotate them manually, to make sure that the function is e.g. pure). Non-template functions don't have their attributes inferred.
Dec 12 2016
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2016-12-12 12:15, Nicholas Wilson wrote:

 there is the pure function attribute, how ever this still allows you to
 use globals *if you pass them as parameters to the function*.
And it can access immutable global data. -- /Jacob Carlborg
Dec 12 2016
parent Nikhil Jacob <nikhiljaco gmail.com> writes:
On Monday, 12 December 2016 at 12:30:42 UTC, Jacob Carlborg wrote:
 On 2016-12-12 12:15, Nicholas Wilson wrote:

 there is the pure function attribute, how ever this still 
 allows you to
 use globals *if you pass them as parameters to the function*.
And it can access immutable global data.
Thank you all for the help
Dec 12 2016