Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>(Byte)1 + (Byte)2</code> would result in a byte type, regardless of the parenthesis.</p> <p><code>(Byte)1 + (Byte)2 instanceof Integer</code> fails since it would first evaluate "2 instanceof Integer" to true and then try to cast true to Byte resulting in a cast exception.</p> <p>UPDATE 1/1/2014: I took a closer look since it's a valid question as to why the assert doesn't produce a cast error too, here is what I found. Given this script:</p> <pre><code>(Byte)1+(Byte)2 instanceof Integer assert (Byte)1+(Byte)2 instanceof Integer </code></pre> <p>According <a href="http://groovy.codehaus.org/Compiler+Phase+Guide" rel="nofollow">to the documentation</a>, the Groovy compiler parses the script and creates an AST representation of the code based on a grammar. The first line is parsed into an <code>ExpressionStatement</code> (specifically a <code>CastExpression</code>) but the second line is parsed into an <code>AssertStatement</code>. An <code>AssertStatement</code> in Groovy's AST has a <code>BooleanExpression</code> child and it seems without clarifying parenthesis, this influences how it decides to parse the code... the two lines end up looking like this:</p> <pre><code>((1 + ((2) as Byte) instanceof Integer) as Byte) assert ((1) as Byte) + ((2) as Byte) instanceof Integer : null </code></pre> <p>This is why the assert runs and ends up being true when the other results in a GroovyCastException. I'm not sure if this is a bug or not... I'm going to ask on the Groovy forum I think.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload