www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is inc function part of the library ?

reply Alain De Vos <devosalain ymail.com> writes:
I wanted to use the inc function (increment by one) but it is not 
recognised.
So searched google "inc dlang" but that returned nothing 
informative.

Normally I would issue a "grep" in some files to find if a 
function exists.

Is "inc" part as function of the library someonewhere and more 
important, a practical question, how can I do an extensive search 
in the library to functions when i know more or less their name , 
but don't know their exact place as module or package.
May 13 2021
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote:
 I wanted to use the inc function (increment by one) but it is 
 not recognised.
 So searched google "inc dlang" but that returned nothing 
 informative.

 Normally I would issue a "grep" in some files to find if a 
 function exists.

 Is "inc" part as function of the library someonewhere and more 
 important, a practical question, how can I do an extensive 
 search in the library to functions when i know more or less 
 their name , but don't know their exact place as module or 
 package.
It's not a function. It's an operator: ```d int i = 10; ++i; ```
May 13 2021
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote:

 important, a practical question, how can I do an extensive 
 search in the library to functions when i know more or less 
 their name , but don't know their exact place as module or 
 package.
In the search bar at the top of the page, enter your search term(s), select "Library" from the drop down.
May 13 2021
prev sibling next sibling parent Berni44 <someone somemail.com> writes:
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote:
 [...] how can I do an extensive search in the library to 
 functions when i know more or less their name , but don't know 
 their exact place as module or package.
An alternative to the official documentation is [dpldocs](http://dpldocs.info/experimental-docs/dpldocs.home.html) from Adam. It is inofficial (and sometimes a little bit outdated), but it has a different search algorithm and often points you to places, you may have missed using the official documentation.
May 13 2021
prev sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 13 May 2021 at 11:21:58 UTC, Alain De Vos wrote:
 I wanted to use the inc function (increment by one) but it is 
 not recognised.
 So searched google "inc dlang" but that returned nothing 
 informative.

 Normally I would issue a "grep" in some files to find if a 
 function exists.

 Is "inc" part as function of the library someonewhere and more 
 important, a practical question, how can I do an extensive 
 search in the library to functions when i know more or less 
 their name , but don't know their exact place as module or 
 package.
Do you mean atomic increment? 🤔
May 13 2021
parent reply Alain De Vos <devosalain ymail.com> writes:
Writing an inc function is a fascinating voyage.
A function on module level did not worked because it had no this 
context.

This works:
```
void main(){
ref int inc2(ref int x) return pure nothrow  nogc  safe{
	++x;
	return x;
}
int x=0;
inc2(inc2(x));
writeln(x);	
```

The thing is i don't find the function pure.
May 13 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 13 May 2021 at 12:56:49 UTC, Alain De Vos wrote:
 Writing an inc function is a fascinating voyage.
 A function on module level did not worked because it had no 
 this context.

 This works:
 ```
 void main(){
 ref int inc2(ref int x) return pure nothrow  nogc  safe{
 	++x;
 	return x;
 }
 int x=0;
 inc2(inc2(x));
 writeln(x);	
 ```

 The thing is i don't find the function pure.
Take a look at https://dlang.org/phobos/core_atomic.html#.atomicOp
May 13 2021
parent reply Alain De Vos <devosalain ymail.com> writes:
Shouldn't the compiler error it is not pure ?
Or have I a wrong understanding of pure or the compiler.
May 13 2021
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 13 May 2021 at 13:30:29 UTC, Alain De Vos wrote:
 Or have I a wrong understanding of pure or the compiler.
pure means it doesn't depend on any mutable info outside its arguments. You are only working on the arguments there so it is ok.
May 13 2021
parent reply kdevel <kdevel vogtner.de> writes:
On Thursday, 13 May 2021 at 13:45:50 UTC, Adam D. Ruppe wrote:
 On Thursday, 13 May 2021 at 13:30:29 UTC, Alain De Vos wrote:
 Or have I a wrong understanding of pure or the compiler.
pure means it doesn't depend on any mutable info outside its arguments. You are only working on the arguments there so it is ok.
Then D's pure does not match up with WP's definition [1] of pure, at least not 2. The function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams). WP quotes Bartosz Milewski: 2. A function has no side effects. Calling a function once is the same as calling it twice and discarding the result of the first call. [1] https://en.wikipedia.org/wiki/Pure_function
May 13 2021
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 13 May 2021 at 17:48:34 UTC, kdevel wrote:
 Then D's pure does not match up with WP's definition [1] of 
 pure, at least not
Yeah, D's pure is actually useful without being a huge hassle. Makes it into a useful building block that can be used inside other scenarios than the purely pure does. If you want immutable, that's a separate thing in D. You can use it on its own, or combine it with D pure to get to the full formal pure definition.
May 13 2021
prev sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 13 May 2021 at 17:48:34 UTC, kdevel wrote:
 On Thursday, 13 May 2021 at 13:45:50 UTC, Adam D. Ruppe wrote:
 On Thursday, 13 May 2021 at 13:30:29 UTC, Alain De Vos wrote:
 [...]
pure means it doesn't depend on any mutable info outside its arguments. You are only working on the arguments there so it is ok.
Then D's pure does not match up with WP's definition [1] of pure, at least not 2. The function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams). WP quotes Bartosz Milewski: 2. A function has no side effects. Calling a function once is the same as calling it twice and discarding the result of the first call. [1] https://en.wikipedia.org/wiki/Pure_function
Just fyi: https://dlang.org/articles/safed.html
May 13 2021
parent reply kdevel <kdevel vogtner.de> writes:
On Thursday, 13 May 2021 at 21:41:48 UTC, Imperatorn wrote:
[...]
 [1] https://en.wikipedia.org/wiki/Pure_function
Just fyi: https://dlang.org/articles/safed.html
Replied to the wrong post?
May 13 2021
parent Alain De Vos <devosalain ymail.com> writes:
I have the feeling it is a pragmatic definition of pure. (isClose)
Although a compiler message , which does not even be a warning, 
is always a nice to have.
May 13 2021
prev sibling parent drug <drug2004 bk.ru> writes:
13.05.2021 16:30, Alain De Vos пишет:
 Shouldn't the compiler error it is not pure ?
 Or have I a wrong understanding of pure or the compiler.
The function is pure. If you call it several times passing the same argument it will return the same result. https://run.dlang.io/is/futqjP
May 13 2021