Is there a way to generate a compiler error?

I want to determine the type of Nibble.

I want that if the user sets a value higher than 0xfthat, he should generate a compiler error.

Is it possible?

+3
source share
4 answers

No, this is not possible with the C # compiler.

It does not have a preprocessor for testing constant values, such as a C / C ++ preprocessor.

Best of all, you can throw an exception at runtime.

Edit: you can always try running C # code (with some minor changes) through the C / C ++ preprocessor and emit a directive #error.

Edit:

, -, , langauge, - . : , LISP, C/++ ..

0

USER DEVELOPER, :

#if YOUT_VALUE == 0xf
    #error YOUR_ERROR_MESSAGE
#endif

comparsion #if, defied/undefined.

+1

Enum [Flags]. , :

[Flags]
enum Nibble
{
_0,
_1,
// ...
_A,
_F,
};

byte b = Nibble._1|Nibble._A;

int nibble. , .

, API # 4.0.

0

, , , - . , , Algol68 (Algol68, Pascal, Modula-2, Oberon, Component Pascal) (Turbo Pascal, Borland Pascal, FreePascal, Delphi, Kylix, Object Pascal), . , ++, -fu. , , Scala, Haskell, ML, Agda, Epigram, Guru.

, . , , , , .

Another opportunity is the Fortress. In Fortress, various types of integer fixed widths are not actually built into the language; they are user-defined. Thus, there is no reason why you cannot create your own custom integer type of fixed width.

0
source

Source: https://habr.com/ru/post/1755964/


All Articles