How does groovy distinguish division from strings?

Viewed 220

Groovy supports / as a division operator:

groovy> 1 / 2
===> 0.5

It supports / as a string delimiter, which can even be multiline:

groovy> x = /foo/
===> foo
groovy:000> x = /foo
groovy:001> bar/
===> foo
bar

Given this, why can't I evaluate a slashy-string literal in groovysh?

groovy:000> /foo/
groovy:001>

clearly groovysh thinks this is unterminated for some reason.

How does groovy avoid getting confused between division and strings? What does this code mean:

groovy> f / 2

Is this a function call f(/2 .../) where / is beginning a multiline slashy-string, or f divided by 2?

1 Answers
Related