www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can opApply be made nogc?

reply solidstate1991 <laszloszeremi outlook.com> writes:
Since it's a bit difficult to make tree traversal through range 
(especially if someone wants to make it  nogc), I thought I'll 
make it through opApply override, however the delegate passed by 
it doesn't have the  nogc attribute, which would automatically 
make it incapable to be used in a  nogc context.

I also don't know if it would work with structs instead of 
classes, since they're easier to handle in a  nogc situation.
Oct 19 2018
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 20/10/2018 12:32 PM, solidstate1991 wrote:
 Since it's a bit difficult to make tree traversal through range 
 (especially if someone wants to make it  nogc), I thought I'll make it 
 through opApply override, however the delegate passed by it doesn't have 
 the  nogc attribute, which would automatically make it incapable to be 
 used in a  nogc context.
 
 I also don't know if it would work with structs instead of classes, 
 since they're easier to handle in a  nogc situation.
class Foo { uint[2] array; int opApply(scope int delegate(ref uint) nogc dg) nogc { int result = 0; for (int i = 0; i < array.length; i++) { result = dg(array[i]); if (result) break; } return result; } } void test() { Foo a = new Foo(); a.array[0] = 73; a.array[1] = 82; foreach (uint u; a) { printf("%d\n", u); } }
Oct 19 2018
prev sibling next sibling parent Alex <sascha.orlov gmail.com> writes:
On Friday, 19 October 2018 at 23:32:44 UTC, solidstate1991 wrote:
 Since it's a bit difficult to make tree traversal through range 
 (especially if someone wants to make it  nogc), I thought I'll 
 make it through opApply override, however the delegate passed 
 by it doesn't have the  nogc attribute, which would 
 automatically make it incapable to be used in a  nogc context.

 I also don't know if it would work with structs instead of 
 classes, since they're easier to handle in a  nogc situation.
https://forum.dlang.org/thread/erznqknpyxzxqivawnix forum.dlang.org
Oct 19 2018
prev sibling parent welkam <wwwelkam gmail.com> writes:
DIP 1000 says:

Delegates currently defensively allocate closures with the GC. 
Few actually escape, and with scope only those that actually 
escape need to have the closures allocated.

https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md#benefits
Oct 21 2018