Re: [Vala] Another interesting List error



Hi Cayle,

On Don, 2006-08-31 at 08:52 -0500, Cayle Graumann wrote:
I have a language question now that you bring it up,  when should I
use var and when should I specify the type, when I was reading the C#
documentation, it seemed like var was just an unnecessary new keyword
(MS does seem to have keyword-itis when it comes to C#) to let a
programmer be lazy about specifying the type.  I can understand that
the compiler already knows the type when used with the new command,
and so it's not absolutely necessary to specify it on the left hand
side, but can var be used with the return types of a function? 

Yes, var can be used with normal return values, too.



            From our simplified example, in the main program would you
do this:

                         List<ListElement> l = lc.makelist(); // You
know what l is

            or would you do this: 

                         var l = lc.makelist(); // You don't
necessarily know what l is without looking at the makelist
documentation.

It depends on the context. You probably shouldn't just blindly use var
wherever it's possible, e.g. writing var instead of int doesn't buy you
anything and I have intentionally not allowed to use var as method
return type. There are quite a few situations where it's appropriate to
use var, though.

I'll probably always use var when creating objects or arrays as it
doesn't increase readability if the same type name is twice on the same
line. It's a matter of style, it shouldn't be used to be able to write
cryptic code but it may make some code easier to write.

Jürg




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]