由原始类型引起的代码重复:如何避免精神错乱? [英] Code duplication caused by primitive types: How to avoid insanity?

查看:63
本文介绍了由原始类型引起的代码重复:如何避免精神错乱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个Java项目中,由于Java处理(非)原语的方式,我受到代码重复的困扰.在必须将相同的更改手动复制到四个不同的位置( int long float double )之后,em>再次,在第三次再次再次,我真的很接近(?)捕捉.

In one of my Java projects I am plagued by code repetition due to the way Java handles (not) primitives. After having to manually copy the same change to four different locations (int, long, float, double) again, for the third time, again and again I came really close (?) to snapping.

以各种形式,此问题已不时出现在StackOverflow上:

In various forms, this issue has been brought up now and then on StackOverflow:

共识似乎收敛于两个可能的选择:

The consensus seemed to converge to two possible alternatives:

  • 使用某种代码生成器.
  • 你能做什么? C'est la vie!

好吧,第二个解决方案是我现在正在做的事情,对于我的理智而言,它正逐渐变得危险,就像

Well, the second solution is what I am doing now and it is slowly becoming dangerous for my sanity, much like the well known torture technique.

问了这些问题已经过去了两年,Java 7也问世了.因此,我希望有一个更简单和/或更标准的解决方案.

Two years have passed since these questions were asked and Java 7 came along. I am, therefore, hopeful for an easier and/or more standard solution.

  • 在这种情况下,Java 7是否有任何更改可以缓解 strain 的变化?在简明的变更摘要中找不到任何内容,但是也许某个地方有一些晦涩的新功能?

  • Does Java 7 have any changes that might ease the strain in such cases? I could not find anything in the condensed change summaries, but perhaps there is some obscure new feature somewhere?

虽然可以选择源代码生成,但我更喜欢使用标准JDK功能集支持的解决方案.当然,使用 cpp 或其他代码生成器都可以,但是添加更多依赖项,并需要更改构建系统.

While source code generation is an alternative, I'd prefer a solution supported using the standard JDK feature set. Sure, using cpp or another code generator would work, but it adds more dependencies and requires changes to the build system.

JDK似乎唯一支持这种类型的代码生成系统是通过注释机制.我设想了一个处理器,它将像这样扩展源代码:

The only code generation system of sorts that seems to be supported by the JDK is via the annotations mechanism. I envision a processor that would expand source code like this:

@Primitives({ "int", "long", "float", "double" })
@PrimitiveVariable
int max(@PrimitiveVariable int a, @PrimitiveVariable int b) {
    return (a > b)?a:b;
}

理想的输出文件应包含此方法的四个请求的变体,最好带有相关的Javadoc注释e.t.c.是否有注释处理器可以处理这种情况?如果没有,那么建造一个会怎样?

The ideal output file would contain the four requested variations of this method, preferrably with associated Javadoc comments e.t.c. Is there somewhere an annotation processor to handle this case? If not, what would it take to build one?

也许最近又出现了一些其他花样?

Perhaps some other trick that has popped up recently?

重要说明:除非有理由,否则我不会使用原始类型.即使在现在,在某些应用程序中使用盒装类型也会对​​性能和内存产生非常实际的影响.

An important note: I would not be using primitive types unless I had a reason. Even now there is a very real performance and memory impact by the use of boxed types in some applications.

max()为例,可以使用在所有数字框类型中都可用的 compareTo()方法.这有点棘手:

Using max() as an example allows the use of the compareTo() method that is available in all numeric boxed types. This is a bit trickier:

int sum(int a, int b) {
    return a + b;
}

在不实际编写六到七次的情况下,如何支持所有数字盒装类型的这种方法呢?

How could one go about supporting this method for all numeric boxed types without actually writing it six or seven times?

推荐答案

如果我仍然想要原始的话,我倾向于使用像 long double 这样的超级类型".性能通常非常接近,并且避免了产生很多变化.顺便说一句:64位JVM中的寄存器无论如何都将是64位.

I tend to use a "super type" like long or double if I still want a primitive. The performance is usually very close and it avoids creating lots of variations. BTW: registers in a 64-bit JVM will all be 64-bit anyway.

这篇关于由原始类型引起的代码重复:如何避免精神错乱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆