此外,Phobos 似乎没有办法做到这一点std.stream
。问题是这std.stream
似乎已被弃用。这是一个使用sscanf
with的示例double
,尽管我不知道如何使用 with real
:
extern (C) int sscanf(const char* input, const char* format, ...);
auto tryParseDouble(string input) {
import std.string;
import std.typecons;
double result;
if (sscanf(input.toStringz, "%lf".toStringz, &result) == 1) {
return Nullable!double(result);
} else {
return Nullable!double();
}
}
void main() {
import std.algorithm;
import std.range;
import std.stdio;
auto myArray = ["foo", "bar", "3.14", "42"];
auto x = myArray
.map!(tryParseDouble)
.filter!(x => !x.isNull)
.front
.get;
writeln(x);
}