Outside array bounds

vino.B bheeman.vino at hotmail.com
Sat Jul 7 15:25:51 UTC 2018


On Saturday, 7 July 2018 at 12:13:21 UTC, Alex wrote:
> On Saturday, 7 July 2018 at 11:22:38 UTC, Timoses wrote:
>>
>> Aw, got it. So args is actually a tuple type where accessing 
>> beyond the defined tuple (T) is invalid?
>>
>>       auto a = [1, 2, 4];
>>       // works
>>       pragma(msg, typeof(a[3]));
>>
>>       auto t = tuple(3, 4, 5.3);
>>       // ERROR:
>>       // pragma(msg, typeof(t[3]));
>
> Yes. The absence of the next element in the array, doesn't 
> matter, while asking its type, while the absence of the next 
> tuple element means also the absence of any type, as I 
> understand it.

Hi All,

   If we replace the statement as args[$ -1] the program works are 
expected, if we apply the same logic in different approach it 
does not work, in the below code if we command the first block 
"if (fnID == "ListFilesNames") {} " then the second "if (fnID == 
"ListFilesSize") {}" works fine, and vice versa but if both the 
"if" block is un-commented it does not work , rather it passes 
wrong parameter to the function. Tried change the args as args[$] 
, args[1], args[2],

Error:
Test.d(31): Error: function Test.ListFilesSize(string FFs, int 
Size) is not callable using argument types (string, Array!string)
VarTest.d(31):        cannot pass argument _param_1 of type 
Array!string to parameter int Size
Test.d(42): Error: template instance 
`Test.process!(Array!string)` error instantiating


Code:
import std.stdio: writeln;
import std.container.array;
import std.algorithm: filter, map;
import std.typecons: Tuple, tuple;
import std.file: dirEntries, isFile, SpanMode;
import std.conv: to;
import std.datetime.systime: Clock, SysTime;
import std.parallelism: parallel, taskPool, TaskPool;
import std.array: empty;

auto ListFilesNames (string FFs) {
     auto dFiles = Array!(Tuple!(string, SysTime))(dirEntries(FFs, 
SpanMode.shallow).filter!(a => a.isFile).map!(a => tuple(a.name, 
a.timeCreated)));
     return dFiles;
}

auto ListFilesSize(string FFs, int Size) {
     writeln(Size);
     auto dFiles = Array!(Tuple!(string, ulong))(dirEntries(FFs, 
SpanMode.shallow).filter!(a => a.isFile).map!(a => tuple(a.name, 
(a.size > Size).to!ulong)));
     return dFiles;
}
void process(T ...)(string fnID, T args) {
if (fnID == "ListFilesNames") {
alias scRType = typeof(ListFilesNames(string.init));
auto PFresult = taskPool.workerLocalStorage!scRType();
foreach (string FFs; args[0]) { PFresult.get ~= 
ListFilesNames(FFs); }
foreach(i; PFresult.toRange) { writeln(!i[][].empty); }
}
else if (fnID == "ListFilesSize") {
alias scRType = typeof(ListFilesSize(string.init, ulong.init));
auto PFresult = taskPool.workerLocalStorage!scRType();
foreach (string FFs; args[0]) { PFresult.get ~= 
ListFilesSize(FFs, args[$ - 1]); }
foreach(i; PFresult.toRange) { writeln(!i[][].empty); }
}
}

void main() {
Array!string NameDir, SizeDir;
NameDir.insert("C:\\Temp\\BACKUP1");
SizeDir.insert("C:\\Temp\\TEAM1");
int Size = 1;
string fnID1 = "ListFilesNames", fnID2 = "ListFilesSize";
process(fnID1, NameDir);
process(fnID2, SizeDir, Size);
}

From,
Vino.B



More information about the Digitalmars-d-learn mailing list