I want to define a Nibble type.
I want that if the user sets the value higher than 0xf, it should generate a compiler error.
Is this possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, this is not possible with the C# compiler.
It has no preprocessor to test for constant values like the C/C++ preprocessor can.
Best you can do, is to throw an exception at runtime.
Edit: You could always try running the C# code (with some minor alterations) through the C/C++ preprocessor and emit an
#errordirective.Edit:
Seeing this is language-agnostic, yes, you can easily do it in any langauge that supports some kind of macro expansion or compile time evaluation. Eg: Scheme, LISP, C/C++, etc.