Jump to page: 1 2
Thread overview
Foreach/opApply with @nogc
Aug 24, 2014
Stefan Frijters
Aug 24, 2014
ketmar
Aug 24, 2014
bearophile
Aug 24, 2014
ketmar
Aug 24, 2014
bearophile
Aug 24, 2014
ketmar
Aug 24, 2014
bearophile
Aug 24, 2014
H. S. Teoh
Aug 24, 2014
ketmar
Aug 24, 2014
Stefan Frijters
Aug 24, 2014
Ali Çehreli
Aug 24, 2014
ketmar
Aug 24, 2014
Stefan Frijters
Aug 24, 2014
ketmar
August 24, 2014
Should this be possible? I admit to not being fully clear on the way delegates are handled, but maybe someone can shed some light? As an example I use a snippet Ali uses to demonstrate opApply:

struct NumberRange {
  int begin;
  int end;

  int opApply(int delegate(ref int) @nogc operations) @nogc const {
    int result = 0;

    for (int number = begin; number != end; ++number) {
      result = operations(number);

      if (result) {
        break;
      }
    }
    return result;
  }
}

void main() {
  import std.stdio;
  foreach (element; NumberRange(3, 7)) { // line 21
    write(element, ' ');
  }
}

When I compile this with 2.066.0 I get

opapply.d(21): Error: function opapply.NumberRange.opApply (int delegate(ref int) @nogc operations) const is not callable using argument types (int delegate(ref int __applyArg0) @system)

It doesn't actually complain about anything gc, just about the signature, so I was wondering if there is some restriction inside the foreach implementation that can be lifted? Thanks in advance for any hints!
August 24, 2014
On Sun, 24 Aug 2014 13:22:49 +0000
Stefan Frijters via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> wrote:

@nogc is a part of signature. gc-function can't call @nogc-one. the same is with calling @system function from @safe one, for example. or impure function from pure.

so to say, foreach() creates implicit delegate withoit '@nogc' attribute, that's why compiler complains.

there is currenly no way to specify attributes for such implicit delegates. this will work, but you'll not be able to call writeln():

  nogc:
  void main() {
     import std.stdio;
     foreach (element; NumberRange(3, 7)) { // line 21
       //write(element, ' '); // this will fail with
         // @nogc function 'z00.main.__foreachbody1' cannot call
         // non-@nogc function 'std.stdio.write!(int, char).write'
    }
  }

what we need here is something like:

  foreach (element; NumberRange(3, 7) @nogc) { ... }

but alas...


August 24, 2014
ketmar:

> there is currenly no way to specify attributes for such implicit
> delegates.

It could be a nice language enhancement.

Bye,
bearophile
August 24, 2014
On Sun, 24 Aug 2014 13:58:45 +0000
bearophile via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> > there is currenly no way to specify attributes for such implicit delegates.
> It could be a nice language enhancement.
i agree. but i tend not to fill enhancement requests without corresponding patches, and i'm not yet familiar with D frontend enough to create the patch.


August 24, 2014
ketmar:

> but i tend not to fill enhancement requests without
> corresponding patches,

I agree that having a patch ready is much better. But people like me file hundreds of ERs without too much damage done, and many of them get eventually implemented. If you find a D limitation, then putting this information in Bugzilla is better than letting this get forgotten.

Bye,
bearophile
August 24, 2014
On Sun, 24 Aug 2014 14:34:02 +0000
bearophile via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> putting this information in Bugzilla is better than letting this get forgotten.
so it can be forgotten in Bugzilla. ;-)


August 24, 2014
ketmar:

> so it can be forgotten in Bugzilla. ;-)

And in ten, or one hundred or one thousand years the whole D language will be forgotten. There are various levels of remembering and forgetting. Putting bugs and ERs in databases is a different kind of forgetting.

Bye,
bearophile
August 24, 2014
On Sun, Aug 24, 2014 at 05:49:29PM +0300, ketmar via Digitalmars-d-learn wrote:
> On Sun, 24 Aug 2014 14:34:02 +0000
> bearophile via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> wrote:
> 
> > putting this information in Bugzilla is better than letting this get forgotten.
> so it can be forgotten in Bugzilla. ;-)

Some of us regularly comb through old bugzilla issues to find things to test & fix. It may take a long time, but we do try. :-)

(And we could use a lot of help -- there are only a few of us and a ton
of old bugs.)


T

-- 
Perhaps the most widespread illusion is that if we were in power we would behave very differently from those who now hold it---when, in truth, in order to get power we would have to become very much like them. -- Unknown
August 24, 2014
On Sun, 24 Aug 2014 08:02:40 -0700
"H. S. Teoh via Digitalmars-d-learn"
<digitalmars-d-learn@puremagic.com> wrote:

> Some of us regularly comb through old bugzilla issues to find things to test & fix. It may take a long time, but we do try. :-)
> 
> (And we could use a lot of help -- there are only a few of us and a
> ton of old bugs.)
i understand that. it was a joke, i'm not trying to being rude or ranting about some of my "pet bugs". ;-)


August 24, 2014
On Sunday, 24 August 2014 at 14:34:03 UTC, bearophile wrote:
> ketmar:
>
>> but i tend not to fill enhancement requests without
>> corresponding patches,
>
> I agree that having a patch ready is much better. But people like me file hundreds of ERs without too much damage done, and many of them get eventually implemented. If you find a D limitation, then putting this information in Bugzilla is better than letting this get forgotten.
>
> Bye,
> bearophile

I would also prefer to submit a patch but I do not have the skills to do it at the moment, so I filed an enhancement request: https://issues.dlang.org/show_bug.cgi?id=13370 .
« First   ‹ Prev
1 2