You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Alexey Afanasiev (JIRA)" <ji...@apache.org> on 2017/08/22 10:30:00 UTC
[jira] [Comment Edited] (GROOVY-8293) Instanceof inference does
work on fields
[ https://issues.apache.org/jira/browse/GROOVY-8293?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16136609#comment-16136609 ]
Alexey Afanasiev edited comment on GROOVY-8293 at 8/22/17 10:29 AM:
--------------------------------------------------------------------
_if (str instanceof String)_ should means nothing for compiler in case of field access. It could be easily rewritten in another thread. It's strange for single threaded app too, example:
{code}
import groovy.transform.CompileStatic
@CompileStatic
class Foo {
Object str = "str"
def bar() {
if (str instanceof String) {
method()
str.toUpperCase() // here should be compile error
}
}
def method() {
str = new Object()
}
}
{code}
Should code above be compiled?
If you say "You're telling it that the type is a String". In code bellow i have assignment, is it not the same? Should this code be compiled(it doesn't compile now)?
{code}
import groovy.transform.CompileStatic
@CompileStatic
class Foo {
Object str = "str"
def bar() {
str = "some str"
str.toUpperCase()
}
}
{code}
My point is compiler couldn't do any type inference correct on fields. At least, there is inconsistency in applying some rules, and ignoring others.
was (Author: alexey afanasiev):
_if (str instanceof String)_ should means nothing for compiler in case of field access. It could be easily rewritten in another thread. It's strange for single threaded app too, example:
{code}
import groovy.transform.CompileStatic
@CompileStatic
class Foo {
Object str = "str"
def bar() {
if (str instanceof String) {
method()
str.toUpperCase() // here should be compile error
}
}
def method() {
str = new Object()
}
}
{code}
Should code above be compiled?
If you say "You're telling it that the type is a String". In code bellow i have assignment, is it not the same? Should this code be compiled(it doesn't compile now)?
{code}
import groovy.transform.CompileStatic
@CompileStatic
class Foo {
Object str = "str"
def bar() {
str = "some str"
str.toUpperCase()
}
}
{code}
My point is compiler couldn't do any type inference correct on fields. At least, there is inconsistency in applying some rules, and ignoring anothers.
> Instanceof inference does work on fields
> ----------------------------------------
>
> Key: GROOVY-8293
> URL: https://issues.apache.org/jira/browse/GROOVY-8293
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation, Static Type Checker
> Affects Versions: 2.4.12
> Reporter: Alexey Afanasiev
>
> This code should not compile:
> {code}
> import groovy.transform.CompileStatic
> @CompileStatic
> class Foo {
> Object str = "str"
> def bar() {
> if (str instanceof String) {
> str.toUpperCase() // here should be compile error
> }
> }
> }
> {code}
> Documentation http://docs.groovy-lang.org/next/html/documentation/core-semantics.html#type-inference states:
> _it does not perform any kind of type inference on fields, always falling back to the declared type of a field_
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)