Hello Ayende,
There ARE two rules for vars, and you CAN set severity for each of them.
First rule is the conservative one - use var when type is explicitly visible
on the right side of initialization expression. This covers cases like this:
var dict = new Dictionary<A,B>();
var list = (IList<A>)obj;
var service = serviceProvider.GetService<ISomeService>();
Second rule suggests var whenever type on the right side is the same as variable
type, e.g. replacing with var will not change types.
First rule is suggestion by default, the second is hint. If you don't like
the second, hit Alt-Enter on the hint, select "Change severity" and select
"Do not show".
Sincerely,
Ilya Ryzhenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
AR> Jimmy,
AR> I honestly think that in some directions, C# 2.0 was pushed as far
AR> as it
AR> could go.
AR> See NHQG for details, but be sure to bring the lang spec with you
AR> :-)
AR> R# 4 supports 3.0, the problem is that I think that it needs better
AR> granularity on the rules for var.
AR> It needs at least two, always use var where appropriate. Use var for
AR> new /
AR> const assignment.
AR> On 2/24/08, Jimmy Bogard
AR> <jimmy.bogard@...> wrote:
AR>
>> Count is valid:
>>
>> if you're importing System.Linq
>> if you import the System.Enumeration namespace
>> if you use the Count() method
>> If R# doesn't support 3.0 in an EAP at least, we can't begin to
>> assume
>> what will or will not be abused. I'm still explaining to folks how
>> generics
>> work in .NET, let alone lambdas.
>> var foo = GetFoo() is a special case. For the overwhelming most part
>> I see var foo = new FooBar();
>>
>> Didn't this happen with C# 2? Everyone assumed all the new features
>> would get abused. If anything people pushed the limits of the
>> features' usefulness to find the boundaries. I don't think we've
>> completely exhausted the usefulness of C# 2.0, what makes everyone
>> think we're already in the hole for 3.0?
>>
>> On Sat, Feb 23, 2008 at 8:30 PM, Chris Brandsma
>> <chris.brandsma@...>
>>
>> wrote:
>>
>>> No Count is valid if you have System.Linq in your usings.
>>>
>>> This is where Extension Methods will make things a bit confusing for
>>> people.
>>>
>>> On Sat, Feb 23, 2008 at 7:28 PM, Ayende Rahien
>>> <Ayende@...> wrote:
>>>
>>>> var shouldn't be smarter, it is string[]. and that doesn't have a
>>>> Count property.
>>>>
>>>> var foo = GetUsersByGroups();
>>>>
>>>> Now, can you tell me what this returns?
>>>>
>>>> On 2/24/08, Jay R. Wren
>>>> <jrwren@...> wrote:
>>>>
>>>>> I knew I'd stir stuff up.
>>>>>
>>>>> I'm sure this is going to be a greater style war than { after the
>>>>> if or on the next line.
>>>>>
>>>>> I'm familiar with GetFoo(). Heck, by name it had better return a
>>>>> Foo, so I know that foo is a Foo.
>>>>>
>>>>> One thing I noticed pretty quickly with var is that the compiler
>>>>> types the variable exactly as the return parameter of the
>>>>> signature of the
>>>>> method. For example,
>>>>> var s= "a b c ".Split(' ');
>>>>> var c = s.Count;
>>>>> If s is an IList<string> this compiles, but s isn't an
>>>>> IList<string>, even though it could be. s here will always be a
>>>>> string[]
>>>>> because Split returns string[]. string[] doesn't have a Count
>>>>> member, so
>>>>> this code does not compile.
>>>>> :( var could be smarter.
>>>>>
>>>>> --
>>>>> Jay
>>>>> On Sat, Feb 23, 2008 at 9:09 PM, Ayende Rahien
>>>>> <Ayende@...> wrote:
>>>>>
>>>>>> Hm, I am not sure that I agree.
>>>>>>
>>>>>> var foo = GetFoo();
>>>>>>
>>>>>> This isn't clear to me. I want to see the type itself in this
>>>>>> scenario.
>>>>>>
>>>>>> On 2/24/08, Jay R. Wren
>>>>>> <jrwren@...> wrote:
>>>>>>
>>>>>>> I take a day off and I miss a good discussion.
>>>>>>>
>>>>>>> I just want to comment that I plan on using var EVERYWHERE that
>>>>>>> I can possibly use it in C# 3.0.
>>>>>>>
>>>>>>> var makes me typing easier.
>>>>>>>
>>>>>>> That is all.
>>>>>>> --
>>>>>>> Jay
>>>>>>> On Fri, Feb 22, 2008 at 6:07 PM, Matthew Podwysocki <
>>>>>>> matthew.podwysocki@...>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Right, which I should have explained a little better...
>>>>>>>>
>>>>>>>> On Fri, Feb 22, 2008 at 6:06 PM, Dustin Campbell <
>>>>>>>> dustinc@...> wrote:
>>>>>>>>
>>>>>>>>>> (I'm not sure which F# uses).
>>>>>>>>>>
>>>>>>>>> F# is statically-typed and uses compile-time type inference,
>>>>>>>>> but to a
>>>>>>>>> greater degree than C# 3.0.
>>>>>>>>> Best Regards,
>>>>>>>>> Dustin Campbell
>>> --
>>> --------------------------------
>>> Christopher Brandsma
>>> http://www.ChrisBrandsma.com
>>> http://www.ElegantCode.com
>>> http://www.BoiseCodeCamp.com
>>> Boise Code Camp 2008 -- March 8, 2008