"Semantic error" in formula


#1

I recently got a “semantic error” when i set up a formula to calculate some values.

It seems that, within a formula, a term must not begin with a single number (without decimal point)…
So, this leads to an error
semantic1

while this is fine
semantic2

and this also works
semantic3

Bug or feature?


#2

By design. GP Script is strongly typed with a little bit of hinting where it makes sense.

Consider what happens if you have

var test : integer
      test = 1.5

In the current design, the first value seen in an expression is considered to be the TYPE of the entire expression. Hence, if you write 1 + x, x must be an integer type because 1 is.

In your example, you would need to write

test2 = 1 + round(test1)


#3

Rereading your initial statement

a term must not begin with a single number (without decimal point)…

that is not an accurate statement of the rule. For example,

Var  test1 : integer
       test2 : integer

test2 = 1 + test1

will work perfectly well

A single number without decimal point is of type integer. Numbers with decimal points in them are not integer Numbers just like the string “1” is completely different from the integer number 1 which is completely different than the floating point number 1.0

I have added some automatic type casting for convenience where it makes sense (which is why you can add an integer to a double) but generally you have to use functions to convert from one type to another.


#4

Thanks for the explanation… now that i know what’s behind it, it absolutely makes sense.
But if you don’t know about it, then this is quite strange when you stumble upon it.:smirk:


#5

Depends what you’re used to. Some languages have weak or no type checking and will just convert from one for another on the fly. But stronger type checking eliminates certain kinds of bugs and is worth the occasional extra work you have to do.