R# complains about redundant parameters that are not really redundant

R

  1. complains that the following code has a T that is redundant. The problem is that this T is what is telling the code what type to convert to, so it is definitely not redundant. T is a type passed into the method by parameters and tell the code what I want to convert to.


P.S. The line below is the only line of code in the method.

return Row[ ColumnName ].ConvertTo]]>( defaultValue );

6 comments
Comment actions Permalink

Are you ABSOLUTELY sure that explicit type specification is not redundant
and cannot be inferred from parameters?
Please could you provide more complete sample

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Lorenzo Statie" <no_reply@jetbrains.com> wrote in message
news:31454361.36271212017682006.JavaMail.jive@app4.labs.intellij.net...

R

  1. complains that the following code has a T that is redundant. The

problem is that this T is what is telling the code what type to convert
to, so it is definitely not redundant. T is a type passed into the method
by parameters and tell the code what I want to convert to.

>

P.S. The line below is the only line of code in the method.

>

return Row[ ColumnName ].ConvertTo<T>( defaultValue );



0
Comment actions Permalink

And defaultValue is probably the same type as T ;)

0
Comment actions Permalink

The return type of your surrounding method is probably already of type T. Therefore the compiler can automatically determine the which typeargument to pass to ConvertTo, making the argument redundant.

Try the following:

object o = new Int32();
int i = ConvertTo(o);
int j = ConvertTo]]>(o);

Both are OK.

0
Comment actions Permalink

No it can't, generics can only be deferred it from the parameters, not from the return value.

Edited by: XIU on May 29, 2008 6:28 PM

0
Comment actions Permalink

This is the complete method:

public static T GetDataRowColumnValue( this DataRow Row, string ColumnName, T defaultValue ) { try { return Row[ ColumnName ].ConvertTo]]>( defaultValue );
}
catch
{
return defaultValue;
}
}

The T on the "ConvertTo" statement is being flagged as redundant. If I am converting a value, how can T be redundant if it tells the Convert what type to convert my value to? Unless the idea here is to let boxing and unboxing deal with what is returned and I should not ensure that the process does exactly what I want. And as far as inference is concerned, putting a 1 as the default value is not going to help when I want to return a float, decimal, etc. Boxing and UnBoxing will be used then, with all the performance hits of that, which can easily be avoided.

Edited by: Lorenzo Statie on May 29, 2008 8:48 AM

0
Comment actions Permalink

Hello Lorenzo,

If you follow ReSharper advice and remove type parameter, will code compile?

If ConvertTo method looks like this:

T ConverTo(T defaultValue) {...} then compiler can infer type argument T from defaultValue parameter's type, which is also T. Sincerely, Ilya Ryzhenkov JetBrains, Inc http://www.jetbrains.com "Develop with pleasure!" IR> This is the complete method: IR> IR> public static T GetDataRowColumnValue( this DataRow Row, string IR> ColumnName, T defaultValue ) IR> { IR> try IR> { IR>]]> return Row[ ColumnName ].ConvertTo<T>( defaultValue );
IR> }
IR> catch
IR> {
IR> return defaultValue;
IR> }
IR> }
IR> The bold/Italic T is being flagged as redundant. If I am
IR> converting a value, how can T be redundant if it tells the Convert
IR> what type to convert my value to? Unless the idea here is to let
IR> boxing and unboxing deal with the values and I should not ensure
IR> that the process does exactly what I want.
IR>


0

Please sign in to leave a comment.