Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a kind of static print in D?

We do have static branching and traits along with a contract programming features on the other. Contracts are great because of the assert feature of delivering the proper message. And static checking doesn't bring any overhead to a compiled code. So naturally it would be very helpful to have a feature for giving proper error messages in a compile time.

For instance, when one tries to get a cross product of a vector and a differential 1-form with my function, with asserts I can specifically tell him about the issue in terms of domain, not about how the compiler sees it in terms of structures and arrays. Or I can just narrow down function arguments types statically but then the user would have to deal with compiler messages, not mine.

So, the question is: is there some way to print custom messages while compile time?

like image 343
akalenuk Avatar asked Oct 29 '13 06:10

akalenuk


People also ask

What does %d do in Java?

%d: Specifies Decimal integer. %c: Specifies character. %T or %t: Specifies Time and date. %n: Inserts newline character.

Are static variables global in C?

A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static. A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.

What is static data type in C?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

What is a printf in C?

"printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (lexing aka. parsing).


1 Answers

Yes, you can emit custom messages using several ways:

  1. pragma(msg, message) will evaluate the message expression and print the result during compilation.
  2. static assert(expression, message) will cause compilation to be aborted when expression, which is evaluated at compile-time, is false. This is the D equivalent of the C #error preprocessor directive.
  3. During CTFE (Compile-Time Function Execution), uncaught exceptions and assert failures will act as a compile-time error, and emit the attached message as well.
  4. The deprecated keyword can now have an explanation message.
  5. There is currently no way to print an evaluated expression during CTFE interpretation, although ctfeWriteln is proposed as a writeln variant which works during CTFE interpretation.
like image 79
Vladimir Panteleev Avatar answered Sep 30 '22 20:09

Vladimir Panteleev