我想要在我的 Java 中添加更多语法糖!

发布于 2024-07-09 21:17:26 字数 327 浏览 7 评论 0原文

例如在 C# 中属性的语法糖:

private int x;

public int X{
    get { return x; }
    set { x = value; }
}

或者只是

 public int X{ get; set; }

我在 java 中缺少逐字字符串... @"C:\My Documents\" 而不是 "C:\\My Documents\\"

您是否同意 Java 需要更多“糖” “? 有人知道下一个 Java 版本中是否会出现 Sugar 吗?

Syntactic sugar for properties for example in C#:

private int x;

public int X{
    get { return x; }
    set { x = value; }
}

or simply

 public int X{ get; set; }

I am missing verbatim strings in java... @"C:\My Documents\" instead of "C:\\My Documents\\"

Do you agree Java needs more "sugar"? Any one knows is there is sugar coming in next Java versions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

ぃ弥猫深巷。 2024-07-16 21:17:26

“语法糖会导致分号癌。”

——艾伦·佩里斯。 编程警句。

"Syntactic sugar causes cancer of the semicolon."

-- Alan Perlis. Epigrams on Programming.

掩耳倾听 2024-07-16 21:17:26

虽然我不一定同意 Java 的哲学,但我认为向 Java 添加大量语法糖会违背它的哲学。 Java 被认为是一种非常简单、易于推理、结构很少的语言,是编程社区中的最低公分母通用语言。 这很大程度上是对 C++ 复杂性的反应。 除了使其成为内存安全语言所需的东西(主要是垃圾收集)之外,它应该没有什么“魔力”。

如果您想要一种相当高性能的静态类型语言中的更多糖分等,我会说 Java 是不适合您的语言。 您可能最好只使用 C#,或者 D。

While I don't necessarily agree with Java's philosophy, I think that adding lots of syntactic sugar to Java would go against its philosophy. Java is supposed to be a very simple, easy to reason about language with few constructs, kind of a lowest common denominator lingua franca in the programming community. It was largely a reaction to the complexity of C++. There is supposed to be very little "magic" in it, beyond what is needed (garbage collection, mostly) to make it a memory-safe language.

If you want more sugar, etc. in a reasonably high-performance, statically typed language, I would say that Java is just the wrong language for you. You're probably better off just using C#, or maybe D.

标点 2024-07-16 21:17:26

听起来你想要 Groovy ...显然属性正在路上,尽管不是在 Java 7 中(如 @erickson在评论中帮助纠正了我)。

Groovy 确实有不错的字符串糖

Sounds like you want Groovy... Apparently properties are on their way, although not in Java 7 (as @erickson helpfully corrected me about in a comment).

Groovy does have nice string sugar.

稍尽春風 2024-07-16 21:17:26

过去几年我还使用 Java 和 C# 进行开发,发现 C# 在表达能力和强大的语言结构方面是一种更优秀的语言。 Java 语言没有经历与 C# 相同程度的变化和更新,至少速度不同。 我仍然不一定意味着 Java 应该大幅更新,但我们需要 Java 平台上的强大且富有表现力的静态类型语言。 我认为Scala将发展成为这种替代语言,当普通Java无法满足时,我们Java开发人员可以切换到它。

C# 绝对是一种非常棒的语言; 可能是当今“最好的”静态类型语言,但在我看来 Java 仍然是一个优越的平台。 我喜欢 Java 这个平台,我喜欢 C# 这个语言。

I've also developed in both Java and C# the last few years, and find C# a superior language with regards to expressiveness and powerful language constructs. The Java language does not undergo the same degree of changes and updates as C#, atleast not at the same pace. I still don't necessarily mean that Java should be drastically updated, but we need a powerful and expressive statically typed language on the Java platform. I think Scala is going to develop into this replacement language, which we Java developers can switch to when ordinary Java does not cut it.

C# is an absolutely fantastic language; probably the "best" statically typed language these days, but Java is still in my opinion a superior platform. I like Java the platform, and I like C# the language.

离鸿 2024-07-16 21:17:26

根据 Mark Reinhold 在 Devoxx 2008 上的演讲,Java 7 中不会向 Java 添加属性支持。

http://hamletdarcy.blogspot.com/2008/12/java-7-update-from-mark-reinhold-at.html

有关属性的更多信息Java 7 的想法在这里:

http://tech.puredanger.com/java7#property

As per Mark Reinhold's talk at Devoxx 2008, property support will not be added to Java in Java 7.

http://hamletdarcy.blogspot.com/2008/12/java-7-update-from-mark-reinhold-at.html

More info on properties in Java 7 ideas here:

http://tech.puredanger.com/java7#property

儭儭莪哋寶赑 2024-07-16 21:17:26

虽然属性很好,但它们不是 java 的。 我认真地认为 javabean 规范很久以前就关闭了这扇门。 我认为需要更清晰的语法糖案例:

  • 由于缺乏委托/闭包而使用内部类。 当前的语法来自地狱。
  • 作为一阶语言构造的方法,即方法数据类型。
  • 使用泛型进行类型推断
  • 反射 API 中泛型的运行时存在。
  • 活动

While properties are nice, they are not java. I seriously think the javabean spec closed that door ages ago. I think there are clearer cases for syntactic sugar that is needed:

  • Use of inner classes due to lack of delegates/closures. Current syntax is from hell.
  • Methods as first-order language constructs, ie method data types.
  • Type inference with generics
  • Run-time presence of generics in reflection API.
  • Events
半葬歌 2024-07-16 21:17:26

如果我再次使用 java 来做任何大型的事情,它将作为另一个编译器的输出语言。

If I use java for anything large again, it will be as an output language from another compiler.

你与昨日 2024-07-16 21:17:26

那是老式的 C#。 如今是

public int X { get; set; }

That is oldschool C#. Nowadays it is

public int X { get; set; }
在巴黎塔顶看东京樱花 2024-07-16 21:17:26

糖对牙齿有害。 语法糖对你的大脑有害。

Sugar is bad for your teeth. Syntactic sugar is bad for your brain.

放血 2024-07-16 21:17:26

这并不是完全必要的。

一个简单的编辑器宏就可以工作:

prop int x -->

private int x;

public int getX(){
   return x;
}
public void setX(int val){ 
   x = val;
}

编辑:(回应评论)

这比以下可读性如何:

private int x;

public int X{
    get { return x; }
    set { x = value; }
}

This isn't entirely necessary.

A simple editor macro could work:

prop int x -->

private int x;

public int getX(){
   return x;
}
public void setX(int val){ 
   x = val;
}

Edit: (in response to comments)

How is this any less readable than:

private int x;

public int X{
    get { return x; }
    set { x = value; }
}
说谎友 2024-07-16 21:17:26

我已经编写了一些注释(和注释处理器),这对这有很大帮助。

请参阅 http://code.google.com/p/javadude/wiki/Annotations

例如:

@Bean(
    properties={
        @Property(name="name", bound=true), // String is default type
        @Property(name="age", type=int.class, bound=true)
    }
)
public class Foo extends FooGen {}

这会生成包含字段和 get/set 方法的 FooGen,并使它们绑定(这是可选的)。 您还可以用这些做许多其他有趣的事情。

注意:我现在正在做一些调整,弃用各种“覆盖”选项。

享受吧,

——斯科特

I've written some annotations (and an annotation processor) that helps this quite a bit.

See http://code.google.com/p/javadude/wiki/Annotations

For example:

@Bean(
    properties={
        @Property(name="name", bound=true), // String is default type
        @Property(name="age", type=int.class, bound=true)
    }
)
public class Foo extends FooGen {}

This generates FooGen containing the fields and get/set methods, as well as making them bound (which is optional). There are many other fun things you can do with these as well.

NOTE: I'm doing a few tweaks now that deprecate the various "override" options.

Enjoy,

-- Scott

不打扰别人 2024-07-16 21:17:26

Java 可以很好地实现的一件事是相当于 refout 参数。

One thing Java would do really well to implement is something equivalent to ref and out parameters.

殊姿 2024-07-16 21:17:26

我不喜欢“语法糖”,因为这将是另一件事需要学习,并且最终很可能会被滥用。 我曾咒骂过自己制作 getter 和 setter 的烦恼,所以我理解为什么人们希望尽可能简单地创建它们,但我宁愿看到 @Get@Set< /code> 和 @GetSet 注释比更多的语法 thingamajiggers 来完成这项工作。

I don't like "syntactic sugar" as is mainly because it would be yet another thing to learn and would most likely get abused eventually. I have cursed at the annoyance of making getters and setters myself though so I understand why one would want to make creating those as easy as possible but I'd rather see @Get, @Set and @GetSet annotations than more syntax thingamajiggers to do the job.

强者自强 2024-07-16 21:17:26

java平台采用了大量的编程语言。 如果您想要更多语法糖,您可以使用另一种语言,如 Groovy 或 (J)Ruby,它仍然会在 jvm 上运行并与您的其他 java 库一起使用。 (我什至认为有一个 C# 实现;)

The java platform has adopted a huge amount of programming languages. If you want more syntactic sugar you could use another language like Groovy or (J)Ruby and it will still run on the jvm and work with your other java-libraries. (I even think there is a C# implementation ;)

冷弦 2024-07-16 21:17:26

与许多人一样,我坚持管理用 Java 1.4 编写的应用程序(迁移到 1.5 是许多情况下的一种方式)。 所以即使Java 7或8有了新功能我也无法使用它们......无论如何语法糖是好的,它可以帮助代码的编写、阅读和分析。

As with many, i am stuck to manage application written in Java 1.4 (migration to 1.5 is one the way in many cases). So even if Java 7 or 8 get new feature I will not be able to used them... Anyways syntactic sugar is good, it can help the writing, reading and analysis of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文