[D.typesystem] Static (CT) enforce anybody?

Justin Johansson no at spam.com
Wed Sep 1 07:28:58 PDT 2010


On 01/09/10 22:49, Steven Schveighoffer wrote:
> Compile time checks are only available for compile time values. If you
> have a function like this:
>
> void foo(int[] x)
>
> There is no way at compile time to check whether x has a maximum or
> minimum number of elements. But if you have something like this:
>
> void foo(uint N)(ref int[N] x)
>
> Then you can check N, because it is a compile-time value. you can make
> checks via template constraints or static assert statements:
>
> void foo(uint N)(ref int[N] x) if(N >= minvalue)
>
> - or -
>
> void foo(uint N)(ref int[N] x)
> {
> static assert(N >= minvalue, "Error, invalid sized array passed to foo");
> }
>
> Both of these will fail to compile if you pass incorrect data.
>
> -Steve

Thanks Steve,

I'm returning to bomb bay to think about this.  (See my
'Dark Star' post for interpretation)

- Justin :-)


More information about the Digitalmars-d mailing list