Compile-time constness is waaay to strict!

asd a at sd.invalid
Sat Jul 25 04:34:21 PDT 2009


Daniel Keep Wrote:

> >> bool isEmptyString(string str) {
> >>        static if (str == "") return true;
> > 
> > It works if you use 'if' instead of 'static if', oddly.
> > 
> >>        return false;
> >> }
> >>
> >> void main()
> >> {
> >>        static if (isEmptyString(""))
> >>        {
> >>                int x = 1;
> >>        }
> >> }
> >>
> >>
> >> test.d(5): Error: expression str == "" is not constant or does not evaluate to a bool
> >> test.d(11): Error: cannot evaluate isEmptyString("") at compile time
> >> test.d(11): Error: expression isEmptyString("") is not constant or does not evaluate to a bool
> > 
> > Be sure to report this on Bugzilla: http://d.puremagic.com/issues/
> 
> I don't think this is a bug in DMD.
> 
> It can't execute it at compile-time because it CANNOT COMPILE IT.
> 
> str is a run-time argument.  static if requires a compile-time expression.
> 
> You can't feed runtime constructs to compile-time ones.  Evaluating a
> function at compile time doesn't change that.

That's a shame. This distinction doesn't make sense to me -- it is clearly possible to know value of this function at compile time and there's no way to change the result at run time.

If it's not a bug, I've filed it as feature request then: http://d.puremagic.com/issues/show_bug.cgi?id=3209



More information about the Digitalmars-d mailing list