-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GROOVY-8678: allow floating point literals without leading zeroes #1588
Conversation
+1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
I was worried that some of Groovy's special method/property naming capabilities might cause some conflicts but there doesn't seem to be any issues.
The following examples are unchanged:
class Test {
def '42'() {
println 'foo'
}
def '42d'() {
println 'bing'
}
def get42() {
println 'bar'
}
def get42f() {
println 'baz'
}
def call(arg) {
println "Arg: $arg"
}
}
def u = new Test()
u.'42'() // => foo
u
.'42'() // => foo
u.'42' // => bar
u.'42f' // => baz
u.'42d'() // => bing
Currently variants without the single quotes all fail parsing:
// u.42f // Unexpected input: 'u.42f'
// u.42 // Unexpected input: 'u.42'
// u.42() // Unexpected input: 'u.42'
// u.42f() // Unexpected input: 'u.42f'
// u.42d() // Unexpected input: 'u.42d'
With the changes plus the following additional line:
BigDecimal.metaClass.call << { delegate * 2 }
These now all parse with the number being a parameter to the "u" receiver's call method:
u.42f // equivalent to u(0.42f) => Arg: 0.42
u.42 // => Arg: 0.42
u .42 // => Arg: 0.42
u.42() // equivalent to u(0.42G.call())=> Arg: 0.84
try {
u.42f() // or u.42d() for Double.call
} catch(MissingMethodException mme) {
println mme.message // => No signature of method: java.lang.Float.call() is applicable ...
}
Some might find these strange but they all make sense and might be useful in some DSL scenarios.
Certainly the error for the script _0.0g
is now stranger:
MME: No signature of method: ConsoleScript10._0() is applicable for argument types: (BigDecimal) values: [0.0]
rather than Unexpected input: '_0.0g'
.
The only suggestion I had was whether it would be good to see an example of the new style in the doco.
Perhaps add something like assert .5 == new BigDecimal('.5')
after:
https://github.com/apache/groovy/blob/master/src/spec/test/SyntaxTest.groovy#L113
I am very sorry, obviously I clicked the wrong thing 🙈 :) |
It seems the requested changes are empty, I cannot accept them. |
Apologies, there was a glitch in my cut-n-paste and I didn't spot it straight away. Should be fixed now. There is just one line I was suggesting to add to doco plus see single line comment below. |
fixes https://issues.apache.org/jira/browse/GROOVY-8678 Signed-off-by: Harald Fassler <harald.fassler+9974@gmail.com>
ea167a4
to
80895bc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paulk-asert I added what you suggested. Thank you for the review!
Merged, thanks! |
fixes https://issues.apache.org/jira/browse/GROOVY-8678
Signed-off-by: Harald Fassler harald.fassler+9974@gmail.com