#1216 Type of return problem

yachris Sat 18 Sep 2010

Here's the class (vast simplification of the original code, just to show the error):

class Foo
{
  Str[] bar(Str line)
  {
    res := [,]
    line.split().each |v| { res.add(v) }
    return res
  }
}

Here's the test:

class FooTest : Test
{
  Void testBar()
  {
    f := Foo()
    verifyEq(["a", "b", "c"], f.bar("a b c"))
  }
}

Here's the error:

sys::TestErr: Test failed: sys::Str[a, b, c] != sys::Obj?[a, b, c]

It gets the same error if the declaration of res is

Str[] res := [,]

Thanks!

vkuzkokov Sat 18 Sep 2010

It's because type of [,] is Obj?[,]. Type of expression is not inferred by the type of variable it's assigned to.

Str[] res := [,]

changes type of variable, not a list it references. Exception is made for field initialization only.

res := Str[,]

will fork though. But not for fields because they must have explicit type. See #986 for further clarification.

EDT: #986 doesn't clarify much. #269 does better.

yachris Sat 18 Sep 2010

Excellent, thanks!

Login or Signup to reply.