[ 
https://issues.apache.org/jira/browse/GROOVY-10936?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17796774#comment-17796774
 ] 

Leonard Brünings edited comment on GROOVY-10936 at 12/14/23 2:35 PM:
---------------------------------------------------------------------

FYI using {{@CompileStatic}} works, but it is still annoying for code that has 
to be dynamic for other reasons.

{code:groovy}
import java.util.regex.Pattern
import groovy.transform.CompileStatic

class Reproducer {
    @CompileStatic
    static void main(String... args) {
        def random = new Random()

        def staticPatternSlashy = ~/some static pattern \w+/
        def staticPatternGString = ~"some static pattern \\w+"
        def dynamicPatternSlashy = random.nextInt() % 2 == 0 ? ~/pattern one 
\w+/ : ~/pattern two \w+/
        def dynamicPatternGString = random.nextInt() % 2 == 0 ? ~"pattern one 
\\w+" : ~"pattern two \\w+"

        assert staticPatternSlashy instanceof Pattern
        assert staticPatternGString instanceof Pattern
        assert dynamicPatternSlashy instanceof Pattern
        assert dynamicPatternGString instanceof Pattern
    }
}
{code}



was (Author: leonard84):
FYI using {{@CompileStatic}} works, but it is still annoying for code that has 
to be dynamic for other reasons.

{code:groovy}
import java.util.regex.Pattern
import groovy.transform.CompileStatic

class Reproducer {
    @CompileStatic
    static void main(String... args) {
        def random = new Random()

        def staticPatternSlashy = ~/some static pattern \w+/
        def staticPatternGString = ~"some static pattern \\w+"
        def dynamicPatternSlashy = random.nextInt() % 2 == 0 ? ~/pattern one 
\w+/ : ~/pattern two \w+/
        def dynamicPatternGString = random.nextInt() % 2 == 0 ? ~"pattern one 
\\w+" : ~"pattern two \\w+"

        assert staticPatternSlashy instanceof Pattern       // succeeds
        assert staticPatternGString instanceof Pattern      // succeeds
        assert dynamicPatternSlashy instanceof Pattern      // fails
        assert dynamicPatternGString instanceof Pattern     // fails
    }
}
{code}


> Patterns conditionally created using pattern operator ~ are cast to 
> String/GString
> ----------------------------------------------------------------------------------
>
>                 Key: GROOVY-10936
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10936
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 4.0.7, 4.0.8, 4.0.9
>            Reporter: Pavlo Shevchenko
>            Priority: Major
>
> *Summary*
> If the value of the Pattern object is based on some argument evaluated at 
> runtime, then such objects will be created as String/GString instead of 
> Pattern.
> This used to work in 4.0.6, but started failing in 4.0.7.
> *Reproducer*
> {code:java}
> class Reproducer {
>     static void main(String... args) {
>         def random = new Random()
>         def staticPatternSlashy = ~/some static pattern \w+/
>         def staticPatternGString = ~"some static pattern \\w+"
>         def dynamicPatternSlashy = random.nextInt() % 2 == 0 ? ~/pattern one 
> \w+/ : ~/pattern two \w+/
>         def dynamicPatternGString = random.nextInt() % 2 == 0 ? ~"pattern one 
> \\w+" : ~"pattern two \\w+"
>         assert staticPatternSlashy instanceof Pattern       // succeeds
>         assert staticPatternGString instanceof Pattern      // succeeds
>         assert dynamicPatternSlashy instanceof Pattern      // fails
>         assert dynamicPatternGString instanceof Pattern     // fails
>     }
> } {code}
> *Workaround*
> Explicitly declaring the variable as Pattern does not work and fails at 
> runtime as it will attempt to cast the String to Pattern. Creating the 
> pattern using Pattern.compile()  works, but is a nasty refactoring and 
> eliminates the benefits of the operator.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to