Why no nullable var objects?

P

proxyuser

In Troelsen's Pro C# 2008 book, it says

It is permissible... to assign an inferred local variable to null after its
initial assignment (provided it is a reference type):

// OK, is SportsCar is a reference type!
var myCar = new SportsCar();
myCar = null;

....it is illegal to define a nullable implicitly typed local variable using
the C# ? token

// Nope, can't define nullable implicit variables,
// as implicit variables can never be initially assigned
// null to begin with!
var? nope = new SportsCar();


The reason he gives can't be a good reason. Why does it need to be
assignable to null at declaration time to be nullable in general? I don't
see why they couldn't allow

var? n = 0;
n = null;
 
P

Patrice

Hi,

int? a = 10;
var b = a;
b = null;

works. For ? I believe this is more precisely just because type inference is
type inference i.e. "var" does the job including finding out if the inferred
type is nullable or not...
 
P

Peter Duniho

[...]
The reason he gives can't be a good reason. Why does it need to be
assignable to null at declaration time to be nullable in general? I
don't
see why they couldn't allow

var? n = 0;
n = null;

I'm not sure I follow your question precisely. But, the answer to the
last part is simple: "var" is always a reference type, and you can never
make Nullable<T> reference types (there'd be no point...reference types
are always nullable anyway).

Pete
 
N

not_a_commie

I'm not sure I follow your question precisely.  But, the answer to the  
last part is simple: "var" is always a reference type, and you can never  
make Nullable<T> reference types (there'd be no point...reference types  
are always nullable anyway).

Uh, you're saying that

var i = 1;

makes i a reference type? It does not. It makes i an int type -- a
value type.

And just because the question mark operator is not available on null
doesn't mean you can't make a nullable type directly:

var n = new Nullable<int>(0);
 
B

Ben Voigt [C++ MVP]

The reason he gives can't be a good reason. Why does it need to be
assignable to null at declaration time to be nullable in general? I
don't see why they couldn't allow

var? n = 0;
n = null;

The C# language team could have allowed that, but it would have been a brand
new syntax (var is not a type, so type? doesn't automatically include var?)
and lots of extra work for the compiler team. For something that probably
no one would ever use. They decided it wasn't worth the extra work.
 
P

Peter Duniho

Uh, you're saying that

var i = 1;

makes i a reference type? It does not. It makes i an int type -- a
value type.

And just because the question mark operator is not available on null
doesn't mean you can't make a nullable type directly:

var n = new Nullable<int>(0);

Sorry. I claim brain overload due to the 105 degree weather today. :(
 
D

David Anton

You're confusing 'var' with a type.
'var' simply means "hey compiler, figure the type out". It makes no sense
to say "hey compiler, figure the type out but it's System.Nullable".
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
P

proxyuser

David Anton said:
You're confusing 'var' with a type.
'var' simply means "hey compiler, figure the type out". It makes no sense
to say "hey compiler, figure the type out but it's System.Nullable".

I don't think I am confusing the 2. It makes complete sense to me to tell
the compiler to figure out the type, but also tell it it's nullable. The
type would never be Nullable, it would be Nullable<T>, and using var allows
the coder to not have to figure out T (which is precisely the fundamental
reason for desiring var in the first place, no?)

Anyway, I'm still curious about my original question, i.e. the author's
explanation that "implicit variables can never be initially assigned
null to begin with". I don't see what initially assigning a variable to
null has to do with whether the variable can ever be assigned null.

var? n = 0;
n = null;

certainly seems doable by the compiler. So far, I'm accepting Ben's
explanation (I agree it doesn't seem real useful). I do understand that
it's very easy for compiler writers to simply swap "int?" out for
Nullable<int> (assuming they implement Nullable<T> first!), and there is no
such easy thing for "var?". But my real point is I don't understand the
author's comment.
 
P

proxyuser

And just because the question mark operator is not available on null
doesn't mean you can't make a nullable type directly:

var n = new Nullable<int>(0);


Cool, but of course in that case it kind of defeats the purpose of var,
since you have to know the type :)

Actually, I never do this
int? i

I always do this
Nullable<int> i

I like explicitness.
 
D

David Anton

Why would System.Nullable<T> get special treatment?
Then you would also expect System.Collections.Generic.List<T> to get the
same treatment?

It's just the C# short nullable syntax (using the '?' symbol) that is
confusing you. Forget about this special (optional) syntax and look at it
from the longer "System.Nullable<T>" syntax and the whole thing becomes clear.

--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
P

Peter Duniho

And just because the question mark operator is not available on null
doesn't mean you can't make a nullable type directly:

var n = new Nullable<int>(0);

Cool, but of course in that case it kind of defeats the purpose of var,
since you have to know the type :)

I agree. But there are some people who want to avoid writing a type name
at all costs. :) And it's true that "var" can be much more succinct,
even allowing for the "?" shorthand, than writing out the whole type
("int" is a bad example, since the "?" adds only a single character).

Pete
 
P

proxyuser

David Anton said:
Why would System.Nullable<T> get special treatment?
Then you would also expect System.Collections.Generic.List<T> to get the
same treatment?

It's just the C# short nullable syntax (using the '?' symbol) that is
confusing you. Forget about this special (optional) syntax and look at it
from the longer "System.Nullable<T>" syntax and the whole thing becomes
clear.

I'm sorry, I'm not following you at all. I don't find the ? symbol
confusing. I find the author's comment confusing. Do you have an
explanation for it? What does *when* the variable gets assigned null have
to do with *whether* the variable can be assigned null or not?
 
P

Patrice

proxyuser said:
Anyway, I'm still curious about my original question, i.e. the author's
explanation that "implicit variables can never be initially assigned
null to begin with". I don't see what initially assigning a variable to
null has to do with whether the variable can ever be assigned null.

Hi,

He meant you can't do :

var a=null;

As the compiler has no way to infer the type...
 
P

proxyuser

Patrice said:
Hi,

He meant you can't do :

var a=null;

As the compiler has no way to infer the type...

I understand that. But the confusing part is that he said that that is the
reason you can't do var?, and I don't think that's right. I think the
reason you can't do it is simply because the compiler writers didn't
implement it, not because there's any reason it can't be implemented.
 
G

gareth erskine-jones

I understand that. But the confusing part is that he said that that is the
reason you can't do var?, and I don't think that's right. I think the
reason you can't do it is simply because the compiler writers didn't
implement it, not because there's any reason it can't be implemented.

Perhaps they though it was ugly? At it stands, var is very easy to
explain:

var b = "hello";

What is the type of b? It is the type of the rhs - and "hello" is a
string.

var c = 10;

what is the type of c? it is the type of the rhs - and 10 is Int32.

Your suggestion would mean a special case where the variable does not
have the type of the rhs. The c# designers seem to try to aviod
special cases in an attempt to keep the language simple and elegant.

In this case, you would have gained very little functionality, but
would have complicated the syntax of "var" (or rather, you'd actually
have introduced a completely new keyword: "var?").

Another source of confusion would be that:

var? b = 10;

and

int? c = 20;

now look quite similar - while they're actually quite different (the
second being much simpler from the point of view of the compiler).

Perhaps instead of var?, they could add some syntax allowing nullable
literals

var c = ?20;

where ?20 is an int? literal.

GSEJ
 
P

Patrice

Not sure what he said in the book but I agree with you. IMO it has nothing
to do with var a=null not being possible (it looks like as he thought type
inference is done at runtime and can't be done if the actual value is
currently null).

This is more likely because this is not something that looks usefull and it
has likely the potential to introduce some confusion for no benefit
(basically it would just allow to tell that you wan't a nullable type when
the expression you used doestn't allow to infer this ; you can likely do so
anyway by changing the right expression rather than by using this kind of
special notation). So basically it has no interest which is likely a good
reason ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top