Я только начал использовать Dlang, и он кажется идеальным для людей, которые хотят более безопасный язык Си. Кроме того, он имеет современные парадигмы, такие как функциональное программирование.
Я пытаюсь следующий код для преобразования списка чисел в виде строк в список целых чисел:
import std.stdio;
import std.array;
import std.conv;
int[] list_str2int(char[][] slist){ // this function should convert a list of numbers as char[] to integers
int[100] intcol;
int N = cast(int)slist.length;
foreach(int i; 0..N){
char[] temp = slist[i];
temp = split(temp, ".")[0]; // effectively this is floor; to!int does not work if '.' is there;
intcol[i] = to!int(temp); // not working;
}
return intcol; // error from this statement;
}
void main(){
char[][] strlist = cast(char[][])["1.1","2.1","3.2","4.4"];
int[] newintlist = list_str2int(strlist);
writeln("Converted list: ", newintlist);
}
Но получаю следующую ошибку:
$ rdmd testing.d
testing.d(13): Error: returning cast(int[])intcol escapes a reference to local variable intcol
Failed: ["/usr/bin/dmd", "-v", "-o-", "testing.d", "-I."]
Я не могу понять, почему произошла ошибка в строке возврата первой функции, где переменная int [].
Где проблема и как ее можно решить? Спасибо за вашу помощь.