#1710 Inconsistency with type-inference

MoOm Sat 26 Nov 2011

Consider the following sample-code:

class TestTypeInference
{
  Str[] fieldStrs := [,]

  Void main()
  {
    Str[] localStrs := [,]
    echo(fieldStrs.typeof)
    echo(localStrs.typeof)
  }
}

When I run it, I get the following result:

sys::Str[]
sys::Obj?[]

The type-inference does not work the same-way with fields and with local-vars. Is there a reason for this or is this a bug?

brian Sun 27 Nov 2011

It is by design. We made a special exception for fields in ticket 986 so that all fields have an explicit type signature for readability. Everything else uses normal type inference rules.

MoOm Sun 27 Nov 2011

Ok, I got it. Is there a reason why we don't apply the same special treatment for local-vars? I know I can use type-inference with local-vars but it would be more consistent if it would behave the same way.

brian Sun 27 Nov 2011

Ok, I got it. Is there a reason why we don't apply the same special treatment for local-vars?

You don't need it in local variables because they support type inference. So I think it is better to have just have one way:

x := Str[,]     // the one-way  
Str[] x := [,]  // if we supported both ways

Really it is a question of trade-offs: which is better to have one way versus two ways to use type inference in local variables, or to be consistent with fields?

Login or Signup to reply.