Exceptional coding style

Artur Skawina art.08.09 at gmail.com
Tue Jan 15 06:52:29 PST 2013


On 01/15/13 14:25, bearophile wrote:
> Artur Skawina:
> 
>> You can't both avoid template bloat *and* statically check the arguments.
> 
> In a well designed statically compiled language you don't need to instantiate templates and let them generate binary code to statically verify a condition. What's needed to test compile time arguments is just some compile-time computation; there is no need for such computation to leave any trace in the resulting binary.

You do realize that the code I posted in the previous message gave you static
verification at compile time and _zero_ runtime overhead? It's incomplete and
adds a toStringz call because it's just a POC. Actually that call can be dropped,
assuming a template string parameter is always null-terminated (which could be
specced that way), and then:

   void main() {
      printf!"Pi == %d.%d == %g == %s\n"(3, 14, 3.14, "3.14".ptr);
   }

compiles to:

   <_Dmain>:
       55                      push   %ebp
       89 e5                   mov    %esp,%ebp
       83 ec 28                sub    $0x28,%esp
       dd 05 88 d7 06 08       fldl   0x806d788
       c7 44 24 14 64 d7 06    movl   $0x806d764,0x14(%esp)
       08 
       dd 5c 24 0c             fstpl  0xc(%esp)
       c7 44 24 08 0e 00 00    movl   $0xe,0x8(%esp)
       00 
       c7 44 24 04 03 00 00    movl   $0x3,0x4(%esp)
       00 
       c7 04 24 69 d7 06 08    movl   $0x806d769,(%esp)
       e8 4c f2 ff ff          call   8049100 <printf at plt>
       31 c0                   xor    %eax,%eax
       c9                      leave  
       c3                      ret    

You can't get any less overhead than this; not even when using a "well designed
statically compiled language".

Being able to easily force evaluation at compile time and even a compile-time
mutable ephemeral storage class would be good ideas, but are not necessary for
this (they would make some things simpler, eliminate the need for recursion etc - yes).

> Beside the idea of "ghost types" (it's not a Pokemon), see also this little thread:
> 
> http://forum.dlang.org/thread/nxhsgwliuwdgidaoudud@forum.dlang.org
> 
> D needs ways to avoid some template bloat in the first place, not just to remove it later.
> 
> In some cases I'd like to write code like this, but to not let this generate different template instantiations (this means x is really a run-time value, but it's required to be known at compile-time):
> 
> 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) {}'?


artur


More information about the Digitalmars-d mailing list