You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thodoris Sotiropoulos (Jira)" <ji...@apache.org> on 2021/03/13 12:24:00 UTC

[jira] [Updated] (GROOVY-9979) Inconsistency in handling of numeric types

     [ https://issues.apache.org/jira/browse/GROOVY-9979?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thodoris Sotiropoulos updated GROOVY-9979:
------------------------------------------
    Description: 
I have the following program
{code:java}
public class Main {
  public static void foo() {
    final Byte f = (Float) 32.3 // possible loss of precision
    baz((Float) 13.3) // cannot find matching method
  }
  
  public static Byte bar() {
    return (Float) 4324141.4 // does not report error.
  }

  public static void baz(Byte x) {}
}
{code}
In the above program, during the assignment `final Byte f = (Float) 32.3` (line 3), I get
{code:java}
test.groovy: 4: [Static type checking] - Possible loss of precision from java.lang.Float to java.lang.Byte
 @ line 4, column 20.
       final Byte f = (Float) 32.3 // possible loss of precision
                      ^1 error

{code}
Similarly, when passing a Float value in a function expecting Byte (line 4), I get
{code:java}
test.groovy: 4: [Static type checking] - Cannot find matching method Main#baz(java.lang.Float). Please check if the declared type is correct and if the method exists.
 @ line 4, column 5.
       baz((Float) 13.3); // does not report error.

{code}
 

However, the compiler does not complain when returning a Float value in a function whose return type is Byte (line 8),

 

Is this inconsistency a bug or a feature?

  was:
I have the following program
{code:java}
public class Main {
  public static void foo() {
    final Byte f = (Float) 32.3 // possible loss of precision
    baz((Float) 13.3) // does not report error.
  }
  
  public static Byte bar() {
    return (Float) 4324141.4 // does not report error.
  }

  public static void baz(Byte x) {}
}
{code}
In the above program, during the assignment `final Byte f = (Float) 32.3`, I get
{code:java}
test.groovy: 4: [Static type checking] - Possible loss of precision from java.lang.Float to java.lang.Byte
 @ line 4, column 20.
       final Byte f = (Float) 32.3 // possible loss of precision
                      ^1 error

{code}
However, the compiler does not complain when returning a Float value in a function whose return type is Byte (line 8), or passing a Float value in a function expecting Byte (line 4).

 

Is this inconsistency a bug or a feature?


> Inconsistency in handling of numeric types
> ------------------------------------------
>
>                 Key: GROOVY-9979
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9979
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Priority: Major
>
> I have the following program
> {code:java}
> public class Main {
>   public static void foo() {
>     final Byte f = (Float) 32.3 // possible loss of precision
>     baz((Float) 13.3) // cannot find matching method
>   }
>   
>   public static Byte bar() {
>     return (Float) 4324141.4 // does not report error.
>   }
>   public static void baz(Byte x) {}
> }
> {code}
> In the above program, during the assignment `final Byte f = (Float) 32.3` (line 3), I get
> {code:java}
> test.groovy: 4: [Static type checking] - Possible loss of precision from java.lang.Float to java.lang.Byte
>  @ line 4, column 20.
>        final Byte f = (Float) 32.3 // possible loss of precision
>                       ^1 error
> {code}
> Similarly, when passing a Float value in a function expecting Byte (line 4), I get
> {code:java}
> test.groovy: 4: [Static type checking] - Cannot find matching method Main#baz(java.lang.Float). Please check if the declared type is correct and if the method exists.
>  @ line 4, column 5.
>        baz((Float) 13.3); // does not report error.
> {code}
>  
> However, the compiler does not complain when returning a Float value in a function whose return type is Byte (line 8),
>  
> Is this inconsistency a bug or a feature?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)