Skip to content
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

Merged
merged 1 commit into from
Jun 11, 2021

Conversation

nineninesevenfour
Copy link
Contributor

@daniellansun
Copy link
Contributor

+1
/cc @paulk-asert

Copy link
Contributor

@paulk-asert paulk-asert left a 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

@nineninesevenfour
Copy link
Contributor Author

nineninesevenfour commented Jun 11, 2021

I am very sorry, obviously I clicked the wrong thing 🙈 :)

@nineninesevenfour
Copy link
Contributor Author

It seems the requested changes are empty, I cannot accept them.
Is there something open for me to do?

@paulk-asert
Copy link
Contributor

paulk-asert commented Jun 11, 2021

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.

Copy link
Contributor Author

@nineninesevenfour nineninesevenfour left a 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!

@paulk-asert paulk-asert merged commit 8ad3f0e into apache:master Jun 11, 2021
@paulk-asert
Copy link
Contributor

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants