www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to replace Exception with as a macro in C?

reply yisooan <yisooan invalidemail.com> writes:
I wonder there is the way as I said in the title. For instance,

in C,
 #define indexInvalidException Exception("The index is invalid")
 
 /* Do something with the macro here */
 if (false)
   indexInvalidException;
This is allowed. But I want to do the exact same thing in D. I have already tried some expressions with alias? but it doesn't work.
 alias indexInvalidException = Exception("The index is invalid");
 
 /* Use the alias in somewhere */
 if (false)
   throw new indexInvalidException;
Would you help me, please?
Feb 18
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 19 February 2019 at 05:50:04 UTC, yisooan wrote:
 This is allowed.
 But I want to do the exact same thing in D. I have already 
 tried some expressions with alias? but it doesn't work.
alias can't be used for expressions.
 Would you help me, please?
There's nothing exactly equivalent to the C preprocessor. The closest you'd be able to get without using a keyword like mixin is to use a function: ``` Exception invalidIndexException() { throw new Exception("Index is invalid"); } if(false) invalidIndexException; ``` Of course, you can also subclass Exception: ``` class InvalidIndexException : Exception { this(string file = __FILE__, size_t line = __LINE__) { super("Index is invalid", file, line); } } if(false) throw new InvalidIndexException; ```
Feb 18
parent Mike Parker <aldacron gmail.com> writes:
On Tuesday, 19 February 2019 at 06:16:59 UTC, Mike Parker wrote:

 ```
 Exception invalidIndexException() { throw new Exception("Index 
 is invalid"); }
Eh, that should be: void invalidIndexException() {...}
Feb 18
prev sibling parent Marco de Wild <admin localhost.com> writes:
On Tuesday, 19 February 2019 at 05:50:04 UTC, yisooan wrote:
 I wonder there is the way as I said in the title. For instance,

 in C,
 #define indexInvalidException Exception("The index is invalid")
 
 /* Do something with the macro here */
 if (false)
   indexInvalidException;
This is allowed. But I want to do the exact same thing in D. I have already tried some expressions with alias? but it doesn't work.
 alias indexInvalidException = Exception("The index is 
 invalid");
 
 /* Use the alias in somewhere */
 if (false)
   throw new indexInvalidException;
Would you help me, please?
Alias is just a nickname for a symbol (i.e. alias string = immutable(char)[];) and lets you refer to that symbol with the nickname. You can't supply runtime parameters (like constructor parameters), but you can give compile-time template parameters (as the result of applying those parameters is a symbol rather than a value). Macros on the other hand, do a textual replacement, i.e. they alter the source code that goes into the compiler. Depending on how brief you want to make your code, you can either use classic inheritance class IndexInvalidException : Exception { this() { super("The index is invalid") } } or, closest as you can get to macros: enum throwNewIndexInvalidException = `throw new Exception("The index is invalid");`; void main() { mixin(throwNewIndexInvalidException); } String mixins transform a string into an expression, and paste the expression wherever it is mixed in. It gets precompiled rather than substituted in the source code. This means it's best to have the whole line into a string, instead of individual parts (you can't have a mixin with just `Exception("The index is invalid")` for example).
Feb 18