有没有办法生成编译器错误?

时间:2022-02-14 09:44:24

I want to define a Nibble type.

我想定义一个Nibble类型。

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

我希望如果用户将值设置为高于0xf,它应该生成编译器错误。

Is this possible?

这可能吗?

4 个解决方案

#1


0  

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

不,使用C#编译器是不可能的。

It has no preprocessor to test for constant values like the C/C++ preprocessor can.

它没有预处理器来测试像C / C ++预处理器那样的常量值。

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 #error directive.

编辑:您可以通过C / C ++预处理器尝试运行C#代码(进行一些小的更改)并发出#error指令。

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.

看到这是与语言无关的,是的,您可以在任何支持某种宏扩展或编译时评估的语言中轻松完成。例如:Scheme,LISP,C / C ++等。

#2


1  

If in your point USER is a DEVELOPER you can do this with macro like this:

如果在您的观点中USER是开发人员,您可以使用以下宏执行此操作:

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

But in some development enviroment you may have problem with comparsion in #if statement, because her functionality was cuted into defied/undefined only.

但是在某些开发环境中,你可能会在#if语句中遇到比较问题,因为她的功能仅限于defied / undefined。

#3


0  

You could use an Enum with the [Flags] attribute. This way you are allowed to use bitwise operations on its members:

您可以使用带有[Flags]属性的Enum。这样,您可以对其成员使用按位运算:

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

byte b = Nibble._1|Nibble._A;

You could also create a struct nibble with an implicit conversion operator from int to nibble. But this would create a runtime error, not a compile time error.

您还可以使用从int到nibble的隐式转换运算符创建结构半字节。但这会产生运行时错误,而不是编译时错误。

If you want to do static checking have a look at c#4.0 Contracts API.

如果你想进行静态检查,请查看c#4.0 Contracts API。

#4


0  

It looks like the easiest way to achieve what you want is a subrange type. Languages which support subrange types are pretty much all languages in the Algol68 line of succession (Algol68, Pascal, Modula-2, Oberon, Component Pascal) and their cousings and derivatives (Turbo Pascal, Borland Pascal, FreePascal, Delphi, Kylix, Object Pascal) as well as Ada. I believe that you can implement subrange types in C++ using some heavy template-fu. You can probably implement them in languages with more expressive type systems, such as Scala, Haskell, ML, Agda, Epigram, Guru.

看起来实现您想要的最简单的方法是子范围类型。支持子范围类型的语言几乎是Algol68连续系列中的所有语言(Algol68,Pascal,Modula-2,Oberon,Component Pascal)及其外壳和衍生物(Turbo Pascal,Borland Pascal,FreePascal,Delphi,Kylix,Object Pascal) )以及阿达。我相信你可以使用一些重模板fu在C ++中实现子范式。您可以使用具有更具表现力的类型系统的语言来实现它们,例如Scala,Haskell,ML,Agda,Epigram,Guru。

I have no idea why not more languages support subrange types. They are obviously useful, easy to use, easy to understand, easy to implement.

我不知道为什么更多语言不支持子范围类型。它们显然有用,易于使用,易于理解,易于实现。

Another possibility might be Fortress. In Fortress, the various fixed-width integer types are not actually built into the language, they are user-defined. So, there is no reason why you should not be able to build your own user-defined fixed-width integer type.

另一种可能是Fortress。在Fortress中,各种固定宽度的整数类型实际上并未构建在语言中,它们是用户定义的。因此,没有理由不能构建自己的用户定义的固定宽度整数类型。

#1


0  

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

不,使用C#编译器是不可能的。

It has no preprocessor to test for constant values like the C/C++ preprocessor can.

它没有预处理器来测试像C / C ++预处理器那样的常量值。

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 #error directive.

编辑:您可以通过C / C ++预处理器尝试运行C#代码(进行一些小的更改)并发出#error指令。

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.

看到这是与语言无关的,是的,您可以在任何支持某种宏扩展或编译时评估的语言中轻松完成。例如:Scheme,LISP,C / C ++等。

#2


1  

If in your point USER is a DEVELOPER you can do this with macro like this:

如果在您的观点中USER是开发人员,您可以使用以下宏执行此操作:

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

But in some development enviroment you may have problem with comparsion in #if statement, because her functionality was cuted into defied/undefined only.

但是在某些开发环境中,你可能会在#if语句中遇到比较问题,因为她的功能仅限于defied / undefined。

#3


0  

You could use an Enum with the [Flags] attribute. This way you are allowed to use bitwise operations on its members:

您可以使用带有[Flags]属性的Enum。这样,您可以对其成员使用按位运算:

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

byte b = Nibble._1|Nibble._A;

You could also create a struct nibble with an implicit conversion operator from int to nibble. But this would create a runtime error, not a compile time error.

您还可以使用从int到nibble的隐式转换运算符创建结构半字节。但这会产生运行时错误,而不是编译时错误。

If you want to do static checking have a look at c#4.0 Contracts API.

如果你想进行静态检查,请查看c#4.0 Contracts API。

#4


0  

It looks like the easiest way to achieve what you want is a subrange type. Languages which support subrange types are pretty much all languages in the Algol68 line of succession (Algol68, Pascal, Modula-2, Oberon, Component Pascal) and their cousings and derivatives (Turbo Pascal, Borland Pascal, FreePascal, Delphi, Kylix, Object Pascal) as well as Ada. I believe that you can implement subrange types in C++ using some heavy template-fu. You can probably implement them in languages with more expressive type systems, such as Scala, Haskell, ML, Agda, Epigram, Guru.

看起来实现您想要的最简单的方法是子范围类型。支持子范围类型的语言几乎是Algol68连续系列中的所有语言(Algol68,Pascal,Modula-2,Oberon,Component Pascal)及其外壳和衍生物(Turbo Pascal,Borland Pascal,FreePascal,Delphi,Kylix,Object Pascal) )以及阿达。我相信你可以使用一些重模板fu在C ++中实现子范式。您可以使用具有更具表现力的类型系统的语言来实现它们,例如Scala,Haskell,ML,Agda,Epigram,Guru。

I have no idea why not more languages support subrange types. They are obviously useful, easy to use, easy to understand, easy to implement.

我不知道为什么更多语言不支持子范围类型。它们显然有用,易于使用,易于理解,易于实现。

Another possibility might be Fortress. In Fortress, the various fixed-width integer types are not actually built into the language, they are user-defined. So, there is no reason why you should not be able to build your own user-defined fixed-width integer type.

另一种可能是Fortress。在Fortress中,各种固定宽度的整数类型实际上并未构建在语言中,它们是用户定义的。因此,没有理由不能构建自己的用户定义的固定宽度整数类型。