digitalmars.D.learn - Template Oriented Programming
Adam <Adam and.Eve> writes:
I'm not sure if "Template Oriented Programming" seems to be the way to go in D, but I've got my head mainly stuck around OOP. I'm a bit confused about how to dive into it. With OOP, we create interfaces, which provide a contract that all implementers of the interface have to abide by. Delegation is done through the interfaces to maximize independence between those that use it. Essentially the contract/interface is the law, and nothing is above it. Now, with TOP, we used templates which are self contained blocks of code that functions similar to generic classes(in fact, identical in some cases). Templates, though, because they can contain compile time logic checking, also can create contracts. Right? It sounds like TOP allows more generic contracts because the programmer has a more powerful constraint syntax than what interfaces define. Interface inclusion is very strict and mainly based off of inclusion(if you include a method in an interface then every implementation also has to include that method). TOP, OTH, allows more loosely based constraints that are up to the programmer(the static if part of a template). Is this the basic idea? Use templates when you want more power? That is, TOP can do everything OOP can do but more? Or are these ultimately two orthogonal concepts?
Sep 17 2015
Justin Whear <justin economicmodeling.com> writes:
On Thu, 17 Sep 2015 20:41:11 +0000, Adam wrote:Is this the basic idea? Use templates when you want more power? That is, TOP can do everything OOP can do but more? Or are these ultimately two orthogonal concepts?I think you've got the right idea. Some unordered thoughts: * OOP provides run-time dynamism while TOP is strictly compile-time. This means that TOP can produce optimal code via inlining and no indirections, but also means that it can't do plugin systems via dynamic libraries. You may, of course, mix the two. * static if allows features to be enabled inline based on the capabilities of the input types * Since D code can generate entirely new code in the compiler, TOP offers some serious potential for cool magic. Use wisely, of course. * TOP moves the information bar back a step--instead of runtime reflection the compiler provides compile-time reflection, allowing runtime reflection to become a userspace library.
Sep 17 2015