Phobos doesn’t appear to have a way to do this without exceptions, besides std.stream
. The problem is that std.stream
seems to be deprecated. Here is an example using sscanf
with double
, though I don’t know how to do it 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);
}