If that were available, even if it were limited to
built-in types, we could write generic algorithms that wouldn??™t care whether they were
working on an int, a long, a double, a decimal, and so forth. Limiting it to the builtin
types would have been disappointing but better than nothing. The ideal solution
would have to also allow user-defined types to act in a numeric capacity??”so you could
define a Complex type to handle complex numbers, for instance. That complex number
could then store each of its components in a generic way as well, so you could
have a Complex
, a Complex, and so on.14
Two related solutions present themselves. One would be simply to allow constraints
on operators, so you could write a set of constraints such as
where T : T operator+ (T,T), T operator/ (T, int)
This would require that T have the operations we need in the earlier code. The other
solution would be to define a few operators and perhaps conversions that must be supported
in order for a type to meet the extra constraint??”we could make it the
???numeric constraint??? written where T : numeric.
One problem with both of these options is that they can??™t be expressed as normal
interfaces, because operator overloading is performed with static members, which
can??™t implement interfaces. It would require a certain amount of shoehorning, in
other words.
Pages:
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230