Structural Search and Replace with unbound Generic Types

A colleague of mine had written some code using C#'s nullable types, and was apparently unaware of the existence of the

??
operator. Instead he used the
GetValueOrDefault
method, specifically the overload that takes one parameter, since in general the type's default values and the logical defaults differed.

I decided to change this to the more idiomatic form. I thought the logical way to go would be to use ReSharper's Structural Search and Replace. Doing that was easy. The search pattern was simply
$exp$.GetValueOrDefault($value$)
and the replace pattern was
$exp$ ?? $value$
, with both exp and value being expression placeholders without type constraints.

That worked well, and I decided it might not be a bad idea to keep this pattern around as a quick fix. However, I would really prefer if exp was constrained to only match expressions that match the unbound type
System.Nullable<T>
. I have not been able to find any way to do that. I tried using the unconstructed name
System.Nullable
, which did not work. I even tried creating a Type placeholder, and using that in the type constraint of the expression placeholder, but that also did not work. Is there some way to add such a constraint using the Structural Search system?

ReSharper team: If it is not currently possible to add such a constraint, please consider adding a way to do so. That seems like a relatively common thing to want to match in a Structural Search and Replace. Also please consider adding a quick fix (probably of hint severity), or even just a context action that performs the above action. That would help to make the idiomatic
??
operator more discoverable.
1 comment

Hello Kevin,

Generic type constraints are not supported by SSR yet, but we have received
similar requests and we're considering to provide such support in one of
the future releases. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

A colleague of mine had written some code using C#'s nullable types,
and was apparently unaware of the existence of the ?? operator.
Instead he used the GetValueOrDefault method, specifically the
overload that takes one parameter, since in general the type's default
values and the logical defaults differed.

I decided to change this to the more idiomatic form. I thought the
logical way to go would be to use ReSharper's Structural Search and
Replace. Doing that was easy. The search pattern was simply
$exp$.GetValueOrDefault($value$) and the replace pattern was $exp$ ??
$value$, with both exp and value being expression placeholders without
type constraints.

That worked well, and I decided it might not be a bad idea to keep
this pattern around as a quick fix. However, I would really prefer if
exp was constrained to only match expressions that match the unbound
type System.Nullable, which did not work. I even tried creating a Type
placeholder, and using that in the type constraint of the expression
placeholder, but that also did not work. The type constraint of an
expression seems limited to bound types. Is there some way to add such
a constraint using the Structural Search system?

ReSharper team: If it is not currently possible to add such a
constraint, please consider adding a way to do so. That seems like a
relatively common thing to want to match in a Structural Search and
Replace. Also please consider adding a quick fix (probably of hint
severity), or even just a context action that performs the above
action. That would help to make the idiomatic ?? operator more
discoverable.

---
Original message URL:
http://devnet.jetbrains.net/message/5299892#5299892



0

Please sign in to leave a comment.