why is ifThrown un at safe?
Bastiaan Veelo
Bastiaan at Veelo.net
Fri Mar 15 19:20:28 UTC 2019
On Friday, 15 March 2019 at 18:46:25 UTC, bauss wrote:
> On Friday, 15 March 2019 at 18:04:05 UTC, Bastiaan Veelo wrote:
>> In the code below (https://run.dlang.io/is/d0oTNi), ifThrown
>> is inferred as un at safe. If instead I write the implementation
>> of ifThrown out (after res2) then it is @safe. As far as I can
>> see, there is no real difference. So why doesn't ifThrown work
>> in this case, and can it be made to work?
>>
>> Thanks!
>>
>> void main() @safe
>> {
>> import std.process;
>> import std.exception;
>>
>> const res1 = execute(["clang", "-v", "-xc++",
>> "/dev/null", "-fsyntax-only"], ["LANG": "C"])
>> .ifThrown((e) @safe {
>> import std.typecons : Tuple;
>> return Tuple!(int, "status", string, "output")(-1,
>> e.msg);
>> }); // Fails
>>
>> const res2 = () {
>> try
>> {
>> return execute(["clang", "-v", "-xc++",
>> "/dev/null", "-fsyntax-only"], ["LANG": "C"]);
>> }
>> catch (Exception e)
>> {
>> import std.typecons : Tuple;
>> return Tuple!(int, "status", string, "output")(-1,
>> e.msg);
>> }
>> }();
>> }
>
> Because the handlers may be unsafe.
But this handler is explicitly marked @safe, and ifThrown is a
template, which should infer its attributes, right?
> There is no safe overload of ifThrown.
Could it be added?
> However you can work around this using @trusted.
I know, but since everything really is safe, I'd rather not imply
that it might not be. Besides, I failed to use @trusted without
introducing a new scope, so I'd have to mark the whole block
where `res1` is used as @trusted. Maybe I did that wrong though.
More information about the Digitalmars-d-learn
mailing list