Why is polymorphism unimportant




















I agree I checked in vs and it doesnt like your overrides. Ok where do the dependency objects exist? Something must know about them. I think i'm basically agreeing with Steven on this one - we need a bit more context as this may not be a generic scenario — Tim Rutter. Show 16 more comments. Active Oldest Votes. It is also unusual, and arguably inconvenient, for a caller of these methods to build up the correct dependency and pass it to the method in order for it to be invoked correctly.

Leave it to the IoC container to do the appropriate dependency resolution: Use constructor injection to create the correct dependency into Classes TypeB and TypeC If there is a secondary requirement to Expose an IDependency on the base class TypeA to consumers, then add an additional abstract property to TypeA of type IDependency but this seems iffy As per Ewan's observation, the repository would need some kind of strategy pattern in order to serve up polymorphic domain entities B or C.

In which case, couple the repository to a factory to do exactly this. The concrete factory would need to be bound to the container in order to tap into Resolve. So putting this all together, you might wind up with something like this: using System; using System. Diagnostics; using Microsoft.

RegisterType typeof TypeB ; myContainer. RegisterInstance factory ; myContainer. GetById ; Debug. Assert evenNumberIsB. Assert oddNumberIsC. Improve this answer. Thank you very much for your time and this comprehensive explanation.

I finally came up with an other solution to this problem. The idea is in essence similar to what you and Erwan suggest, but I achieve the DI differently. I'll post it ASAP! Add a comment. Could whatever it is that knows about the dependencies live behind an interface IDependencyProvider which has a function IDependency GetDependency Type type. GetDependency this. GetType ; Your middle tier then knows nothing about the sub types or the sub dependencies.

Tim Rutter Tim Rutter 4, 2 2 gold badges 19 19 silver badges 43 43 bronze badges. As it allows the installer to potentially set a bad config This approach also allows you to inject your dependencies with unity.

Ewan Ewan 1, 1 1 gold badge 11 11 silver badges 24 24 bronze badges. I do agree with Steven's comment about not injecting into entity type objects generally. But in this case with a hard link between the types, I can't see a good way to invert and have the dependencies as services — Ewan. GetById id ; myDomainObject. AbtractMethod ; And that's it! So four things here : The first one is that I inject the delegate that will create and instance of the service. Then it is thread safe because static member is only written one time at the beginning of the application.

All other accesses will be read. Genetic specificity occurs in many host-parasite systems. Each host can recognize and resist only a subset of parasites; each parasite can grow only on particular hosts.

Biochemical recognition systems determine which matching host and parasite genotypes result in resistance or disease. Recognition systems are often associated with widespread genetic polymorphism in the host and parasite populations. I describe four systems with matching host-parasite polymorphisms: plant-pathogen interactions, nuclear-cytoplasmic conflict in plants, restriction enzymes in bacterial defence against viruses, and bacterial plasmids that compete by toxin production and toxin immunity.

On those abstractions are what we like to call types. There is no such thing as a program without types and the internal representation is a implementation detail that can be used to build abstract types. Ok, so I'll back off and admit that there is one completely typeless language out there - the Lambda Calculus. But what's interesting about LC is not so it's typeless but rather you can use it to build types one of those amazing things really.

Anyhow, back on the question of TCL and types. You say there's only one type that supported by the compiler which is probably true. But from the programmers standpoint, TCL supports and infinite number of types limited only by your imagination. A string can be a powerful data structure in that many many types can be formulated from that type.

If it helps, think of it like:. So now you have a Universal Type where everything just happens to be represented as a string think of how powerful lists have been for Lisp.

Now the compiler is not really sure what to do, because it just sees one type unitype. It is up to you to manage what you do with any particular value given in any particular place in the code. All else being equal, that would be true. However, most industrial languages have leaky type-systems; i. Also, there is a strong advantage to using dynamically typed languages because they are both more productive and more flexible than statically typed languages.

Using TDD drastically reduces the amount of debugging by drastically increasing the amount and frequency of testing. I find coding even in statically typed languages goes much faster with TDD, than without it. The reason is, simply, that there is so little debugging. Statically typed language get much more unwieldy the larger that application. There is no such thing as a perfect type system.

Every new change to the requirements has the possibility of causing previous type structures to become sub-optimal; but changing them is hard work. Using TDD which is the only condition under which I would use a dynamically typed language or any language for that matter The testers certainly will, since all of the normally manual test scripts are automated. The maintainers certainly will because the unit tests provide them with code examples for how every little bit of the system works.

The users certainly will because they'll spend less time documenting, reporting, and working around defects. Again, I disagree. I don't really see any immediately conceivable correlation between a the amount of debugging, and b the amount and frequency of testing. Unless you can offer any statistics to substantiate the implicit claim of correlation, I am inclined to dismiss the above statement as arbitrary. Note that, even if the correlation exists, all things are not being equal, again.

Another arbitrary claim, as I see it. If by TDD you mean the methodology explained in "The Craftsman," then it looks like it takes forever to produce even the simplest of things - considering the task of factoring an integer that is being discussed there, any 8th-grader will have it ready and running in about half an hour, without having ever heard of your silver bullet, and it seems like it takes up a whole day with TDD : As an aside, I must confess that this is the point where I dropped-off reading "The Craftsman" though I still hope I'll have the time to catch-up with it at some point in the future.

I find it impossible to concentrate on solving a problem if one has to suffer the distraction of going back to the test-bed every so often to invent and run another test-case. True - there is no such thing as a perfect type system, and every new change in requirements potentially calls for changes in the type definitions. This change, however, actually captures the necessarily improved understanding of the problem domain.

In dynamically-typed languages, complexity of the system has still increased, but is not advertised. There is still the possibility that everything will look and test OK, but at one point something will creep-up from under the rug to trash weeks of furious coding based on subtle confusion. This would sound like a disadvantage of dynamically-typed languages, if it weren't for the "any language" remark ;P More seriously, though - you can't be serious : Your statement suggests that if you didn't have TDD in place, you should simply sit down and cry.

TDD or not, the most likely picture is that test designers will have to design additional tests to make sure you're not buying "XYZ" gismos for "ABC" bucks apiece, or taking the radius of a triangle or the median of a circle or the vector product of scalars or the eigenvalues of your family name.

Then, again - what's wrong with static typing under TDD? Cheers, D. The "leaky behaviour" is not their only problem.

Compared to languages like the one above, yes. Compared to other statically typed languages with sufficient general type systems, no. Productivity is more or less the same, and flexibility is almost the same. Then you don't use broken statically typed languages, no matter if they are called "industrial" or not; you use either a language with a good static type systems, or a dynamically typed one.

The trade-off is that with a good static type system, you have to write less unit tests and can use tools like QuickCheck that allows you to write "meta unit tests" that go beyond "specificiation by example" , and you have better coverage all possible execution paths for "type unit tests", but you may run very rarely, in my experience into some inflexibilities.

For a dynamic type system, it's the other way round. Which one you use is a matter of personal taste, and the application you're going to write. There is no absolute "best way".

Even if there is one you personally prefer. Note that the concept of "variable" may be different in different languages. Imperative assignable variables are only one flavour. If you need to declare types, no matter if for functions, objects, methods, or whatever, I'd probably use some variation "mandatory type declaration".

Again, variables shouldn't be emphasized here. A language that can figure out types of whatever entity is usually called a language with "type-inference". Which has been mentioned now a few times in this thread Usually, most languages with type-inference make declarations of types optional. You can declare a type of a function, or "variable", or whatever , and then the type-checker will compare it with the inferred type, or use it to specialize the inferred type.

No, but Hindley-Milner style typing is a good approximation. As Marcin has said, there are cases where this is not sufficient, but it really depends on the type of application you write if you need those or not. For most applications, you don't run into trouble. With type-inference, changing them is actually easy: You just change them, and use the type-checker in the same way you would use unit tests to point out those places where you have forgotten to make follow-up changes.

It's hard work for languages where you have to needlessly declare all sort of redundandent information, and then you have the burden to change this redundanent information when you do make the change. BTW, tests through "typing unit tests" are also automated :- If you stop to see types as a burden as they are in some languages , and start thinking of them as a different kind of unit tests, you may get some idea how it can be used.

Just like many other reasonable testing approaches. I feel this is the right time for me to stop biting on TDD :. Also, there is a strong.

Not granted - the productivity of dynamically typed languages is still being challenged, and so are the advantages of their flexibility. Facts please. It doesn't support downcasting checking whether an object is really of some subtype, and if it is, returning a value on which the subtype's operations can be performed. I've not worked on a really enormous application, but I would find that with languages like Modula-3 lots of declarations and strong, static typing I really started to benefit from strong typing as my applications got larger because the types were a ubiquitous type of extra documentation sprinkled throughout the code and that, as changes were made that were inconsistent with other far-off parts of the codebase, the compiler would flag more of those problems for me.

As the programme gets much larger than you can hold much of in your head at once, I quite like having the compiler check an extra dimension of coherency for me. I think that this is something where YMMV, though.

I'm the sort of programmer where extensive typechecking catches most of my bugs. Perhaps, ideally, one should pair these two different sorts of programmer! BCPL is untyped. It can only manipulate machine words, but it has operators which treat the words as pointers or integers or floating point numbers or characters. The Unix shell is untyped. It can only manipulate strings, which can be interpreted as filenames or options or numbers, etc. You can program typefully in any programming language, but that doesn't imply that the language you are using is typed.

If the compiler or the run-time system do not automatically check for type errors then the language isn't typed. The point is that some languages only define one type of data, and it's up to the programmers to build on that to define more if they want them.

It depends on your programming style, I guess. The flexibility granted by a dynamically typed language can be very powerful.

If that power is taken advantage of, then it can lead to greater productivity. I can only assume that at least some percentage of the other programmers in the world will realize similar gains.

Debugging is the act of finding and correcting the bugs in a program. If you have tests for code, there are likely to be fewer bugs. It's a direct relation, even if not necessarily linear. I tend to follow more of a "Test First Programming" approach than the standard "Test Driven Design", but the concept is similar. It doesn't add as much time as you may think it does.

It adds a little bit on the front end, and saves a good amount during the actual coding. It's like sugar and spice and everything n Oh, wait, that's girls. It's really good, though : The point is that the things you see as problems with dynamically typed languages become much less of an issue when using automated testing.

As such, the potential gain to be realized in using a statically typed language is diminished in such a case. That's not to say there isn't such a gain, just that it's not as big as it was The power and productivity gained by the flexibility of dynamically typed languages is a matter of opinion, not fact. And we ignore the benefit of being able to get places faster, what's the point of having cars. The point here is that some of us do see benefits in the flexibility that dynamically typed and untyped languages provide.

Just because you don't see the benefit doesn't make statically typed languages better. Once again, it's opinion. That may be. When that happens, I may be one of the first to sing forth the praise of such languages. Until then, my preference is Tcl : Rob Seeger As a sidenote, I have to hope that the language that breaks the barrier between flexibility and type safety isn't Haskell, or any other "purely" functional language.

Functional Programming, like OOP, is a tool. I despise any language that feels one tool is so perfect that it discards many other good tools, or places them in hard to use holes. That was Chris' very point. And the implication is that even the Tcl programmer has to think in terms of such, let's say "semantic", types in order to write sensible programs, notwithstanding the fact that the language tries everything to sweep them under the carpet. More generally, you always have to consider types, in every programming language.

If the compiler provides guidance to that then it can only be a hindrance if its notion of types is too simplistic for common problems, or if you are forced to explain every annoying detail to it - which, unfortunately, is the situation with type systems in most mainstream languages, but not at all a principal drawback of static typing.

Another thing might be psychology: some people just hate being proven wrong by someone else, especially if that person is just a dumb program. As an aside, I have no issue with compilers providing guidance Especially the "more productive" part. I have programmed extensively in dynamically typed languages, and I think I can safely say that I used to be pretty productive using them. In fact, 15 years ago you could have caught me saying things that sound an awful lot like the things you wrote above.

However, I have learned since, and I now know that I was wrong back then. There is absolutely no way I could improve my productivity by throwing away static types. To illustrate this, let me give an example. I just completed a compiler for a non-trivial language an extended subset of core ML which compiles to native PowerPC assembly code. It took me alone about a month to do so, but that was without being able to work full-time on it. My guess is that without distractions from work and family i.

The total size of the code is just under lines. I fear that if I tried today, I would not be able to complete such a thing at all because I simply don't have extended chunks of time anymore.

My experience with writing non-trivial code in a dynamically typed language is that it takes several hours of uninterrupted time to get anything done.

The absence of distractions is absolutely crucial because one has to keep so much context information while making changes. Nowadays I use the compiler's type checker to maintain that context for me: A change of an invariant in one part of the program is reflected in a change of type, and the type checker will reliably insist on visiting every other place of the program that is affected by this change.

I can easily leave work and go home in the middle of such a change without fear of the next day not remembering anymore what the pending changes were, and which parts of the program still needed an inspection.

But I am not talking about bad statically typed languages. Thats the trick The smaller and more functional the pieces, the easier it is to deal them. The bad ones C, Java, etc. Can you recommend a good, statically typed language that fits the following criteria: - Not strongly functional to the point it makes imperative programming inconvenient - Can easily define new control structures - Type Inference, rather than Manifest - Doesn't force, but allows: - OOP - Functional Programming - Nearly any other style of programming - Doesn't have tons of "special case" rules I hate those with a passion : I'll happily look into new and interesting languages : Rob Seeger.

If you have better tests, you will have a more accurate idea of how much debugging you'll have to do With well designed testing, you could be able to nip it in the bud, most of the time, or you could sacrifice some conceptual coherence wittingly if you did aim for it in the first place, or not if you didn't really care about such things, in general, so the situation rarely manifests itself exactly as I described it.

The cost is still there, though - whether you will identify any incoherent portions of you code and decide to rewrite them, or you just "carry one" for the increased complexity, there's still an increase in the necessary effort. Or, you may have never had to deal with type complexity in the first place.

When the real job boils down to munching strings, numbers, and dates, strong typing could appear to offer a mostly imaginary advantage, or even to be getting in the way. I must disagree. My problem with dynamically typed languages is that they do not provide any reliable means to document complexity in the code, and this is an issue well beyond the scope of testing.

With dynamic typing, every time that you need to recall or validate even a tiny part of the conceptual model, you are on your own to do it on a sheet of paper.

A statically typed language takes a large portion of this burden off your shoulders so a napkin, or even a matchbook, will almost always do ; Cheers, D. You cannot really separate it, because the language is what forces it on the programmer.

In the case of Tcl, "semantic" types primarily exist because every non-trivial operation requires its string arguments to be in certain forms to behave as desired.

Different classes of such forms certainly make up an implicit and fuzzy notion of type, which the programmer's reasoning depends on. You'll still have to debug, but it will take a lot less time.

It's just plain faster finding the where and why for a bug if you have good test coverage The fact that those who test heavily tend to write code that's easy to test and hence, debug is another benefit. So was I. The time needed to whip up the tests required for most code once you're used to doing so is minimal.

For most commands I write, it adds a few minutes to the "up front" time of writing the code. There's quotes there because tests are added as you want to add capability to the code. They're not all written up front. Overall, though, writing the tests doesn't take up much time. I just don't see this, in practice.

Maybe it's because my code is written to be easily testable and, as such, it broken into simple modules I also don't grasp where a statically typed language would provide any help in understanding the "bigger picture" of the software.

That's not, strictly speaking, true. Individual commands may require that string arguments be in a certain form, but the language itself doesn't. It perfectly possible to replace all the commands that take integers and replace them with ones that expect roman numerals instead. The language itself doesn't care. The funny thing is that the car metaphor came to my mind at one point, only the other way around: to me, dynamic typing is the pedestrian way of getting things done - it allows you to "get places faster," provided you don't have to walk more than half a mile ; Anyway, let's shake hands on the "opinion" thing - there appears to be no other way out of this that would be satisfactory for both camps : Cheers, D.

It seems to me that if we led our lives by the rule you've suggested above: "We've lived without it so far, so it can't be that good" How do you reckon people latched on to things such as structured programming, higher-level languages, IDEs, source-level debuggers, etc. Or maybe it's because the problem domain you're working in does not offer that kind of entertainment If you have anything like a conceptual model, you need to understand it, and that includes "tiny parts" thereof - it should all go without saying.

As for how statically typed languages facilitate conceptual modeling - the answer could hardly fit in here, but that's one hell of a good subject for a PhD thesis : Cheers, D.



0コメント

  • 1000 / 1000