Function Template for Dynamic Parameter
vino.B
bheeman.vino at hotmail.com
Sun Jul 1 11:58:30 UTC 2018
On Sunday, 1 July 2018 at 11:52:19 UTC, Alex wrote:
> On Sunday, 1 July 2018 at 11:19:50 UTC, vino.B wrote:
>>
>> Hi Timoses,
>>
>> Thank you very much, can you help me on how to rewrite the
>> below using Variadic template
>>
>> Passing function as a parameter to another function:
>>
>> void ptFun(T)(T function(string, string, int) coRoutine,
>> Array!string Dirlst, ) {
>> alias scRType = typeof(coRoutine(string.init, string.init,
>> int.init));
>>
>> where the "function(string, string, int) coRoutine" should be
>> a variadic function
>>
>> From,
>> Vino.B
>
> I'm not sure, if get your question right, is this what you are
> looking for?
>
>
> ´´´
> import std.stdio;
> import std.traits;
>
> void main()
> {
> alias instantiation = ptFun!(size_t, fun!string);
>
> instantiation(4);
>
> alias instantiation2 = ptFun2!(fun!string);
>
> instantiation2(4);
>
> }
>
> auto fun(T...)(T input)
> {
> return size_t(42);
> }
>
> void ptFun(T, alias funToCall)(size_t definedParam)
> if(is(T == ReturnType!(funToCall)))
> {
> "ptFun called".writeln;
> assert(42 == funToCall("some string"));
> }
>
> void ptFun2(alias funToCall)(size_t definedParam)
> if(__traits(isSame, TemplateOf!(funToCall), fun))
> {
> "ptFun2 called".writeln;
> assert(42 == funToCall("some string"));
> }
> ´´´
Hi Alex,
Something similar to the below code, when compling the below
code i get an error
NewType.d(19): Error: function declaration without return type.
(Note that constructors are always named this)
import core.time: days;
import std.algorithm: each, filter, map, sort, strip;
import std.container.array;
import std.conv: to;
import std.datetime.systime: Clock, SysTime;
import std.file: dirEntries, isFile, SpanMode;
import std.parallelism: parallel;
import std.stdio: File, writefln, writeln;
import std.string: strip;
import std.traits: ReturnType;
import std.typecons: Tuple, tuple;
auto coCleanFiles(T ...)(T args) {
auto dFiles = Array!(Tuple!(string, SysTime))(dirEntries(FFs,
SpanMode.shallow).filter!(a => a.isFile).map!(a => tuple(a.name,
a.timeLastModified)));
return dFiles;
}
void ptManagecoRoutine(T)(T function(T ...)(T args), Array!string
Dirlst) {
alias scRType = typeof(coRoutine(args.init));
auto PFresult = taskPool.workerLocalStorage!scRType;
ReturnType!coRoutine rData;
foreach (string FFs; Dirlst[]) { PFresult.get ~=
coRoutine(FFs.strip); }
foreach(i; PFresult.toRange) { rData ~= i[][]; }
if (!rData[].empty) { rData[].sort!((a,b) => a[1] <
b[1]).each!(e => writefln!"%-83s %.20s"(e[0], e[1].to!string)); }
}
void main () {
Array!string CleanDirlst;
CleanDirlst.insertBack("C:\\Temp\\BACKUP1");
ptManagecoRoutine(&coCleanFiles, CleanDirlst);
}
From,
Vino.B
More information about the Digitalmars-d-learn
mailing list