| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
January 11, 2015 Foreach, return not exist the function. | ||||
|---|---|---|---|---|
| ||||
First time i used foreach with opApply in my classed, and i got this strange behavior.
return e, not exit the function "findClass"
i tried to reproduce it, but it is worked in reproduce example.
but you can read it and read the result of writeln.
I still feel it is my eyes or a small mistake.
---------------------
SrdController findClass(const ClassInfo controllerClass)
{
foreach(e; this) {
if (e.classinfo.name == controllerClass.name) {
writeln("we found " ~ e.classinfo.name);
return e;
}
}
writeln("not found ");
return null;
}
results
------------------
sard.parsers.SrdControllers.add(sard.parsers.SrdControllerNormal)
sard.parsers.SrdControllers.add(sard.parsers.SrdControllerDefines)
we found sard.parsers.SrdControllerNormal
not found
reproduce example here
http://dpaste.dzfl.pl/13fb453d0b1e
it is part for opensource code here
https://github.com/parmaja/sard/blob/master/src/sard/parsers.d#L222
| ||||
January 11, 2015 Re: Foreach, return not exist the function. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zaher Dirkey | On 01/11/2015 12:25 PM, Zaher Dirkey wrote: > reproduce example here > http://dpaste.dzfl.pl/13fb453d0b1e That link doesn't work for me. (?) Does opApply return the delegate's return value ('b' below)? import std.stdio; struct S { int opApply(int delegate(int) dg) { foreach (i; 0 .. 10) { int b = dg(i); if (b) { writefln("Exiting opApply with %s", b); return b; } } return 0; } } int foo() { auto s = S(); foreach (i; s) { writeln(i); return 42; } return 0; } void main() { assert(foo() == 42); } Ali | |||
January 12, 2015 Re: Foreach, return not exist the function. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Sunday, 11 January 2015 at 23:50:18 UTC, Ali Çehreli wrote: > On 01/11/2015 12:25 PM, Zaher Dirkey wrote: > > > reproduce example here > > http://dpaste.dzfl.pl/13fb453d0b1e > > That link doesn't work for me. (?) > > Does opApply return the delegate's return value ('b' below)? > It must return object, but it is in template i added ref, or witout ref, same this the part of opApply int opApply(int delegate(ref T) callback) { int result = 0; for (int i = 0; i < _items.length; ++i) { result = callback(_items[i]); if (result == 0) break; } return result; } this part code here https://github.com/parmaja/sard/blob/master/src/sard/classes.d and reproduced (but works fine) here https://github.com/zaher/d_test/blob/master/foreach.d | |||
January 12, 2015 Re: Foreach, return not exist the function. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Sunday, 11 January 2015 at 23:50:18 UTC, Ali Çehreli wrote:
> On 01/11/2015 12:25 PM, Zaher Dirkey wrote:
>
> > reproduce example here
> > http://dpaste.dzfl.pl/13fb453d0b1e
>
> That link doesn't work for me. (?)
>
> Does opApply return the delegate's return value ('b' below)?
>
> import std.stdio;
>
> struct S
> {
> int opApply(int delegate(int) dg)
> {
> foreach (i; 0 .. 10) {
> int b = dg(i);
> if (b) {
> writefln("Exiting opApply with %s", b);
> return b;
> }
> }
>
> return 0;
> }
> }
>
> int foo()
> {
> auto s = S();
> foreach (i; s) {
> writeln(i);
> return 42;
> }
>
> return 0;
> }
>
> void main()
> {
> assert(foo() == 42);
> }
>
> Ali
It in breaking the loop in opApply
if (result==0) {
must be
if (result) {
Thanks to Ali, and Zor at #D (freenode)
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply