www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - `string[] args` can't be read at compile time?

reply Unazed Spectaculum <unazed spec.org> writes:
import std.stdio;
import std.file;


void main(string[] args)
{
	if (args.length != 2)
	{
		writeln("usage: ./app <path-to-file>");
		return;
	}

	if (!exists!(args[1]))
	{
		writefln("fatal error: %s doesn't exist", args[1]);
	}
}

Line 13 (if (!exists!(args[1]))) produces error:

'app.d(13): Error: variable args cannot be read at compile time'

With 'dmd -run app.d' compilation command. I've seen another 
Stackoverflow post with a similar issue 
(https://stackoverflow.com/questions/39920780/variable-i-cannot-be-r
ad-at-compile-time) but I don't believe it to be related.

Any help?
Dec 10 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 10 December 2017 at 15:28:19 UTC, Unazed Spectaculum 
wrote:
 	if (!exists!(args[1]))
That should be `!exists(args[1])`. You had an extra ! in there by the (. Generally speaking, when there's a "cannot be read at compile time", it is because something is initialized in a static context and/or there's an extra ! in the arg list to get rid of.
Dec 10 2017
parent Unazed Spectaculum <unazed spec.org> writes:
On Sunday, 10 December 2017 at 15:46:57 UTC, Adam D. Ruppe wrote:
 On Sunday, 10 December 2017 at 15:28:19 UTC, Unazed Spectaculum 
 wrote:
 	if (!exists!(args[1]))
That should be `!exists(args[1])`. You had an extra ! in there by the (. Generally speaking, when there's a "cannot be read at compile time", it is because something is initialized in a static context and/or there's an extra ! in the arg list to get rid of.
Thanks! That fixed it. I'll take it into account whenever I see any other future errors like it.
Dec 10 2017