Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit typing; why just local variables?

Tags:

Does anyone know or care to speculate why implicit typing is limited to local variables?

var thingy = new Foo(); 

But why not...

var getFoo() {     return new Foo();  } 
like image 223
Ed Guiness Avatar asked May 05 '09 12:05

Ed Guiness


People also ask

How do you use unassigned local variables?

TLDR: Initialize a variable to null to indicate to the compiler that you plan to assign it later. Suppose you have a situation where you need to create a variable but you will be assigning a value to it within a conditional statement (if, for, foreach, while, etc).

Which keyword can be used to declare implicit type variables to assign a value?

C# var keyword is used to declare implicit type variables.

What is implicit type declaration?

If a name appears in a program and is not explicitly declared, it is implicitly declared. The scope of an implicit declaration is determined as if the name were declared in a DECLARE statement immediately following the PROCEDURE statement of the external procedure in which the name is used.

What is implicitly typed local variables?

Implicitly typed variables are those variables which are declared without specifying the . NET type explicitly. In implicitly typed variable, the type of the variable is automatically deduced at compile time by the compiler from the value used to initialize the variable.


2 Answers

Eric Lippert did an entire blog post on the subject.

  • https://docs.microsoft.com/en-us/archive/blogs/ericlippert/why-no-var-on-fields

In summary, the main problem is that it would have required a major re-architecture of the C# compiler to do so. Declarations are currently processed in a single pass manner. This would require multiple passes because of the ability to form cycles between inferred variables. VB.NET has roughly the same problem.

like image 111
JaredPar Avatar answered Oct 20 '22 16:10

JaredPar


Jared has a fantastic link in his answer, to a fantastic topic.

I think it does not answer the question explicitly.

Why not?

var getFoo() {     return new Foo();  } 

The reason for this is:

What if?

class Foo {}  var GetFoo() {    return GetBar();  }  var GetBar() {   return GetBaz();  }  var GetBaz() {    return new Foo(); } 

You could deduce that GetFoo is going to return Foo, but you will have to trace through all the calls that method makes and its children makes just to infer the type. As it stands the C# compiler is not designed to work in this way. It needs method and field types early in the process before the code that infers types can run.

On a purely aesthetic level I find the var definitions on methods confuse things. Its one place where I think being explicit always helps, it protects you from shooting your self in the foot by accidentally returning a type that causes your signature and a ton of other dependent method signatures to change. Worst still, you could potentially change all you signatures of a method chain without even knowing you did so if you return the value of a method that returns object and happened to be lucky.

I think var methods are best left for dynamic languages like Ruby

like image 40
Sam Saffron Avatar answered Oct 20 '22 18:10

Sam Saffron