Am I misusing with?
Rubn
where at is.this
Sat Jan 19 20:07:34 UTC 2019
On Saturday, 19 January 2019 at 17:49:31 UTC, faissaloo wrote:
> This seems to work fine
>
> file = File("test.txt", "r");
> with (file)
> {
> scope(exit) close();
>
> foreach (string line; file.lines())
> {
> line_array ~= line;
> }
> }
>
> however:
>
> file = File("test.txt", "r");
> with (file)
> {
> scope(exit) close();
>
> foreach (string line; lines())
> {
> line_array ~= line;
> }
> }
>
> Tells me I'm attempting to read from an unopened file, what's
> going on here? It seems like I'm able to use lines() like this
> within with statements unless they're in my foreach iterator.
> Is this a bug or intended behaviour?
If you look at the implementation, "lines" is a struct.
https://github.com/dlang/phobos/blob/v2.084.0/std/stdio.d#L4330
I didn't know you could use structs with UFCS, which is why you
are probably confused as well. It's a function defined in "file".
If you use lines() by itself, you are constructing a new "lines"
struct where the default File is an unopened file.
I tend to avoid "with" altogether because of things like this. It
becomes hard to tell what is actually part of the struct and what
is just UFCS. If you do want to use the object with UFCS you have
to explicitly use the object anyways so the point is kind of mute.
UFCS if you don't know just means you can use global functions
(and apparently structs) with the same syntax as if it were a
member function:
struct testStruct {
this( int value ) {
import std.stdio : writeln;
writeln("testStruct - ", value);
}
}
void test(int value) {
import std.stdio : writeln;
writeln( value );
}
void main() {
10.test(); // prints 10
20.testStruct();
}
https://run.dlang.io/is/0Xpnmt
More information about the Digitalmars-d-learn
mailing list