![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#11
| ||||
| ||||
|
|
I think my question is mostly answered from this last reply. See inline... I do understand the benefits of Generics: the type safety they birng and the time that they save. Right. Things that can't be done just by using interfaces. Again, I ask why? Interfaces may not bring the same level of type safety as List(of T), but they do provide a type of saftey, in that a contraint is being put on the input. I just don't buy that "it can't be done" using Interfaces. It may be harder and it may require more safety's, but I believe it can be done. |
|
Now emp is acting like <T> would in a Gerneic type is it not? No. It's just acting as an interface implementation. I'm sorry, I really don't see the overlap between interfaces and generics here at all. I've been reading Professional .NET 2.0 Generics (wrox) and the author does present interfaces as a pre-Generics way of solving some problems that Generics solve (this example being one of them). |
|
Yes, you can use emp and it'll be strongly typed. However: 1) It will be boxed if the implementation is a value type 2) You can *only* use it as an IEmployee unless you perform casting at runtime (losing the safety). What? Any class that implements IEmployee can be passed into the foo constructor as it, no casting required. And no casting would be required to use it as an IEmployee inside the class either. |
|
For instance, you can't expose another member of the class with the specific IEmployee implementation used by the constructor, because you don't know that at compile-time. Basically, you have no information other than the fact that it's *some* implementation of IEmployee. Sure. But if I'm only interested in the IEmployee members, this is not a problem. My whole premis here is based on the assumption that IEmployee contains the members I want to use. Rememer: I'm already sold on Generics, I'm looking for a devil's advocate agument on the need for them. |
#12
| |||
| |||
|
|
What? Any class that implements IEmployee can be passed into the foo constructor as it, no casting required. And no casting would be required to use it as an IEmployee inside the class either. Suppose I (as a client) want to only use your class with SalariedEmployees. Someone else wants to only use it with ContractEmployees. You (as the class provider) don't know about either of those classes (we've created them ourselves). You only know about IEmployee. |
|
Now: 1) You can't provide a class which assures me at compile time I haven't mixed and matched usages of SalariedEmployee with ContractEmployee. 2) You can only expose employees to me as IEmployee, despite the fact that I *know* I'm only ever going to give you SalariedEmployees. Generics fixes both of these. For instance, you can't expose another member of the class with the specific IEmployee implementation used by the constructor, because you don't know that at compile-time. Basically, you have no information other than the fact that it's *some* implementation of IEmployee. Sure. But if I'm only interested in the IEmployee members, this is not a problem. My whole premis here is based on the assumption that IEmployee contains the members I want to use. Rememer: I'm already sold on Generics, I'm looking for a devil's advocate agument on the need for them. Then don't just present a situation where you *don't* need them. It's like suggesting that reference types are unnecessary because in one particular situation you only need integers. If you're only interested in IEmployee members, and you don't need to allow the client to impose extra type safety (e.g. always using a particular implementation of IEmployee) then generics has no benefit, and just complicates matters. That's very different from saying that you "*could* do what Generics offers with interfaces". |
| -- Jon Skeet - <skeet (AT) pobox (DOT) com http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk |
#13
| ||||||||
| ||||||||
|
|
If you're only interested in IEmployee members, and you don't need to allow the client to impose extra type safety (e.g. always using a particular implementation of IEmployee) then generics has no benefit, and just complicates matters. That's very different from saying that you "*could* do what Generics offers with interfaces". But Generics doesn't help me here either. In your case of *knowing* that you only ever be supplying SalariedEmployees, we'd just change my class to accept SalariedEmployees in its constructor instead of IEmployee. |
|
And, with a Generic: List(of SalariedEmployees) we'd be able to pass a SalariedEmployee just as we would with a: New(x As SalariedEmployee) on my class. |
|
Show me how I couldn't make a class that takes only Salaried Employees then. |
|
I started this (with you) by asking why a parameterized constructor wouldn't satisfy what Gernerics can when you said that I can't do a parameterized type without Generics. |
|
I've provided an example (a parameterized constructor). |
|
Now, you are changing the requirements to need to be able to accept SalariedEmployees |
|
It does not seem that your statement that I can't create a parameterized type using Interfaces is accurate. |
|
Please explain why I'm wrong (you haven't done that). If you simply propose a new scenario as your proof, then I will counter with a new constructor to accomodate your new requirement. |
#14
| |||
| |||
|
#15
| |||
| |||
|
|
I do get your point and I haven't misunderstood anyting (you or the author of the book I'm reading). You've misunderstood what I'm asking. I'm not talking about the semantics of building a reusable class that doesn't need to know at its creation what will be passed into it later. |
|
I get that, but I'm talking about the mechanics of the the parameter passing and what that parameter in the constructor does. Your getting hung up on this. The fact is that (per my first reply to you) I *can* pass a parameter to my type that (mechanically) acts as <T> would in a parameterized type. |
|
But, I do think I've got what I was looking for now. |
#16
| |||
| |||
|
|
I do get your point and I haven't misunderstood anyting (you or the author of the book I'm reading). You've misunderstood what I'm asking. |
|
The fact is that (per my first reply to you) I *can* pass a parameter to my type that (mechanically) acts as <T> would in a parameterized type. |
#17
| |||
| |||
|
|
You know the features that generics bring to the table, but you're thinking in terms of end results, i.e. observable program actions, and you can see that interfaces (or ever System.Object) can do the same thing. But the trouble is, that isn't what generics are about - they aren't about enabling new kinds of end results, to the exclusion of other possible implementations. They're all about static typing. Plain and simple. |
#18
| |||
| |||
|
|
My questions have been about the observable results, not the purpose or under the hood benefits of generics. I am asking for academic reasons. |
#19
| |||
| |||
|
|
Scott M. wrote: My questions have been about the observable results, not the purpose or under the hood benefits of generics. I am asking for academic reasons. Then there's a direct logical contradiction in your position: your questions are for the "reasons", but not the "purpose" - but the purpose is the reason! -- Barry -- http://barrkel.blogspot.com/ |
#20
| |||
| |||
|
|
Scott M. wrote: My questions have been about the observable results, not the purpose or under the hood benefits of generics. I am asking for academic reasons. Then there's a direct logical contradiction in your position: your questions are for the "reasons", but not the "purpose" - but the purpose is the reason! |
|
-- Barry -- http://barrkel.blogspot.com/ |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |