Exceptional coding style
Artur Skawina
art.08.09 at gmail.com
Tue Jan 15 08:13:51 PST 2013
On 01/15/13 16:27, bearophile wrote:
> Artur Skawina:
>
>> static assert(checkArgs!A(fmt), "Invalid or unsupported printf args");
>
> A template constraint is better, I think.
Not necessarily, if you want helpful error messages.
>> You can't get any less overhead than this; not even when using a "well designed
> statically compiled language".<
>
> Maybe if you compile that with dmd and you look in the binary I think you will find some stuff related to that that's not needed.
Never used DMD.
D is designed with an sufficiently smart compiler in mind, for some realistic and
practical definition of a SSC. Therefore failure to perform certain optimizations,
which are reasonably required by the language (like inlining), is a mere quality
of implementation issue.
Yes, it's not the best approach, not relying completely on the optimizer would have
been better, but the D design is as it is, and until that changes blaming the
compiler is the only option.
>>> void foo(int x)(string s) {}
>>
>> A /run-time value known at compile-time/ is an oxymoron.
>>
>> How would your above function to differ from this one
>> 'void foo(alias x)(string s) {}' ?
>> Are you asking for 'void foo(alias int x)(string s) {}'?
>
> That wasn't an important request, feel free to ignore it.
I'm trying to understand what you're asking for.
> What I was trying to say is not impossible. The run-time semantics of this:
>
> void foo(static int x)(string s) {}
>
> Is meant to be the same as:
>
> void foo(int x, string s) {}
>
> The only difference is that the compiler requires x to be statically known (but then you can't use specialize code on the value of x, because that's really not a template function).
>
> This is also allowed, and it's similar to what we were discussing about:
>
> void foo(static int x)(string s) if (predicate(x)) {}
Define "x to be statically known".
void foo()(int x, string s) { import std.stdio; writeln(x, s); }
auto foo(alias X)(string s) /*@inline*/ /*if (__traits(compiles, foo(X, s)))*/ {
return foo(X, s);
}
void main() {
foo!1("one");
foo!2("two");
int three = 3;
foo!three("three");
}
/* The last foo!three case fails with the template constraint enabled,
but /does/ compile w/o it, btw */
I still have no idea *why* you'd want such a thing...
artur
More information about the Digitalmars-d
mailing list