std_exception.html#enforce: example does not compile

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Jan 27 18:34:35 UTC 2018


On Saturday, January 27, 2018 17:57:54 kdevel via Digitalmars-d-learn wrote:
> On Saturday, 27 January 2018 at 16:10:29 UTC, Jonathan M Davis
>
> wrote:
> > On Saturday, January 27, 2018 13:29:00 kdevel via
> >
> > Digitalmars-d-learn wrote:
> >> What's wrong here? And why is the "selective import" of
> >> enforce necessary?
> >
> > Because you named your module enforce. As such, by default,
> > referring to enforce inside of the module refers to the module.
> > Having the selective import overrides that.
>
> Okay. But what about that persisting error message:
>
> zz.d
> ---
> import core.stdc.stdio : fopen;
> import std.stdio : readln, writeln;
> import std.exception; // : enforce;
>
> void main ()
> {
>     auto f = enforce(fopen("data.txt", "r"));
>     auto line = readln(f);
>     enforce(line.length, "Expected a non-empty line.");
> }
> ---
>
> $ dmd zz
> zz.d(8): Error: template std.stdio.readln cannot deduce function
> from argument types !()(shared(_IO_FILE)*), candidates are:
> /.../dmd2/linux/bin64/../../src/phobos/std/stdio.d(3921):
> std.stdio.readln(S = string)(dchar terminator = '\x0a') if
> (isSomeString!S)
> /.../dmd2/linux/bin64/../../src/phobos/std/stdio.d(3955):
> std.stdio.readln(C)(ref C[] buf, dchar terminator = '\x0a') if
> (isSomeChar!C && is(Unqual!C == C) && !is(C == enum))
> /.../dmd2/linux/bin64/../../src/phobos/std/stdio.d(3962):
> std.stdio.readln(C, R)(ref C[] buf, R terminator) if
> (isSomeChar!C && is(Unqual!C == C) && !is(C == enum) &&
> isBidirectionalRange!R && is(typeof(terminator.front ==
> (dchar).init)))
>
> The example still does not compile.

That has nothing to do with enforce. std.stdio.readln does not take a FILE*.
In general, you shouldn't mix core.stdc.stdio and std.stdio.

https://dlang.org/phobos/std_stdio.html#.readln

readln has 3 overloads, all of which read from stdin.

If you want to use readln on a file, then you need to use std.stdio.File and
its member function, readln, not core.stdc.stdio.FILE.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list