Page 1 of 1

possibly incorrect error message

PostPosted: Tue Dec 17, 2013 4:26 am
by kobi7
Code: Select all
Garden.cobra(0,0): Warning: The expression "plant" (of type "IFieldItem") will always evaluate to true because it is not nilable. You can remove the expression.


the expression is:
Code: Select all
if plant


an empty spot in the field is the nil value.
there is a list (actually now multilist) of IFieldItem which is an interface, and not initialized.
I think the warning is wrong.
just a report
kobi

Re: possibly incorrect error message

PostPosted: Tue Dec 17, 2013 4:27 am
by kobi7
the whole method:
Code: Select all
def print
      print "FIELD:"
      for y in .width to int
         for x in .length to int
            plant = .field[x,y]
            if plant
               ch = plant.asChar
               print ch stop
            else
               print c'+' stop
         print

Re: possibly incorrect error message

PostPosted: Tue Dec 17, 2013 4:50 am
by kobi7
my understanding is that since an array cannot instantiate an interface, the cells will have nil values instead (this is a multilist)

the problem also shows as an error:
Code: Select all
   def canPlaceObject(x as int, y as int) as bool
       """the basic rule is the position is vacant/empty"""
      return .field[x,y] is nil


Code: Select all
Error: The left and right sides of the "is" expression can never be identical because of their types ("IFieldItem" and "nil").


I can use a singleton instead, ofcourse, but i think this is a valid usage.
should I just change to "IFieldItem?" everywhere?

Re: possibly incorrect error message

PostPosted: Tue Dec 17, 2013 4:55 am
by kobi7
okay, I decided it would be more correct to indeed use the nullable "IFieldItem?".
however the first warning would still be wrong for an array of interfaces, as the value inside is indeed nil.

Re: possibly incorrect error message

PostPosted: Tue Dec 17, 2013 5:55 am
by Charles
kobi7 wrote:okay, I decided it would be more correct to indeed use the nullable "IFieldItem?".
however the first warning would still be wrong for an array of interfaces, as the value inside is indeed nil.

Perhaps we should say that allowing an array of IFoo is what's wrong and that it has to be an array of IFoo? since it will indeed start with nil. Make sense?

In the future, please include more relevant portions of the source. For example, if Cobra is complaining that the usage of a var, then including the declaration of the var would be informative. The best is always a self contained, small, complete program. But when that's not possible, including all the relevant pieces makes sense.

Re: possibly incorrect error message

PostPosted: Fri Dec 27, 2013 12:20 am
by kobi7
yea, makes sense.
Please accept my apology, I didn't mean to blame anyone. just had too much coffee, lost focus, and was frustrated with slow progress

Re: possibly incorrect error message

PostPosted: Fri Dec 27, 2013 1:17 pm
by Charles
No worries.