Groovy 语法是 Java 语法的精确超集吗? [英] Is Groovy syntax an exact superset of Java syntax?

查看:16
本文介绍了Groovy 语法是 Java 语法的精确超集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一名 Java 程序员,我并没有真正的 Groovy 背景,但我最近经常使用 Groovy 来扩展 Maven(使用 GMaven).到目前为止,我可以在 Groovy 中使用我需要的所有 Java 代码以及添加的 Groovy 糖(元类方法、更多运算符、闭包).我对 Groovy 的了解还很不全面,但我喜欢它,尤其是用于脚本目的(我对在企业场景中使用非静态类型语言有点小心,但这不是这里的主题).

Being a Java programmer, I don't really have a Groovy background, but I use Groovy a lot lately to extend Maven (using GMaven). So far, I could use all the Java code I need in Groovy with the added Groovy sugar (metaclass methods, more operators, closures). My knowledge of Groovy is far from complete, but I like it, especially for Scripting purposes (I'm a bit careful about using a non-static typed language in an enterprise scenario, but that's not the topic here).

无论如何,问题是:

每一点有效的 Java 代码都自动有效的 Groovy 代码吗?(我说的是源代码,不是编译的类,我知道 Groovy 可以与 Java 类交互.)或者是否有 Java 构造在 Groovy 中是非法的吗?也许一个保留的 Groovy 关键字可以用作 Java 中的标识符,或者其他什么?或者 Groovy 是否有意设计为与 Java 100% 源代码兼容?

Is every bit of valid Java code automatically valid Groovy code? (I am talking about Source code, not compiled classes, I know Groovy can interact with Java classes.) Or are there Java constructs that are illegal in Groovy? Perhaps a reserved Groovy keyword that could be used as an identifier in Java, or something else? Or has Groovy deliberately been designed to be 100%-source compatible with Java?

推荐答案

不是.

我最喜欢的不兼容性:文字数组:

My favorite incompatibility: literal arrays:

String[] s = new String[] {"a", "b", "c"};

在 Groovy 中,这种上下文中的花括号应该包含一个闭包,而不是一个文字数组.

In Groovy, curly braces in this context would be expected to contain a closure, not a literal array.

这篇关于Groovy 语法是 Java 语法的精确超集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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