Calling function within class.
Ali Çehreli
acehreli at yahoo.com
Wed Nov 18 21:33:58 UTC 2020
On 11/18/20 7:01 AM, Vino wrote:
> Request your help on how to call a function(listFile) from another
> function(getFilelist) within the same class(GetDirlist), below is an
> example code.
That code looks unnecessarily complex to me. First of all, parallel()
already executes the loop body in separate threads, so I don't see any
reason for Task in that code.
std.parallel will appear during my DConf presentation on Saturday. The
following program applies ideas from some of my slides and just works in
parallel:
import std.process;
import std.exception;
import std.format;
import std.parallelism;
import std.stdio;
class GetDirlist {
@system private auto listFile(immutable string st)
{
auto fl = execute(["ls","-l"]);
enforce(fl.status == 0, format("File not Found: %s", fl.status));
return fl.output;
}
@system public auto getFilelist()
{
// I am using D dynamic arrays for simpliticy
auto flstore = [ "/path1/Dir1", "path2/Dir2" ];
// Note preallocated slot for each result:
auto amidata = new string[flstore.length];
// Taking advantage of automatic loop counter
foreach(i, st; parallel(flstore,1)) {
// Each execution assigns to its own slot
amidata[i] = listFile(st);
}
return amidata[];
}
}
void main() {
// Need an object to call a non-static member function:
auto g = new GetDirlist();
writeln(g.getFilelist());
}
Ali
More information about the Digitalmars-d-learn
mailing list