The Artima Developer Community
Sponsored Link

Weblogs Forum
The Elephant in the Living Room

38 replies on 3 pages. Most recent reply: May 2, 2005 3:11 PM by Terje Slettebø

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 38 replies on 3 pages [ « | 1 2 3 ]
Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: The Elephant in the Living Room Posted: Apr 15, 2005 8:31 AM
Reply to this message Reply
Advertisement
Ah. Well I guess that just goes back to whether you like static typing. Of course, the "advantage" (if you are a static typing fan) of the C++ template approach is that you'll get compile-time feedback if you try to use the function with an object that doesn't support the methods used ("protocol?" -- avoiding the word "interface").

Hmm... Are templates something different than shorthand for an undeclared interface? Maybe they imply an "Annonymous interface?"

In Java, you'd do a max() with two Comparables, except for the fact that you have to also do special cases for all the native types. In C# you could do it without all those special cases. In Python, Ruby, Objective C, etc., you just do it (without the interface).

indranil banerjee

Posts: 38
Nickname: indranil
Registered: Nov, 2004

Re: Templates vs Dynamic Typing Posted: Apr 15, 2005 3:39 PM
Reply to this message Reply
> Templates are kind of a code generation hack to simulate
> dynamic typing.

Not at all, every template is instantiated at compile time and has a static type.

> template <class T> const T& max(const T& t1, const T& t2)
> {
> return t1 > t2 ? t1 : t2;
> }
> class A { int _x; public: A(int x) : _x(x){} };
> max(A(2),A(3)) - won't work

Why should it work? There is nothing in class A that suggests instances should be ordered based on the value of member variable _x.

In Java you have to implement the Comparable interface. In C++ you have to supply the comparison operators. Both cases are statically typed. Although Java max is based on something the type IS (a Comparable). C++ max is based on something the type HAS (an operator>). Subtly, but significantly different.

What would you want max to do for classes like these?
class Point { int _x; int _y; public: A(int x, int y) : _x(x), _y(y) {} };
class Person { int _age; string _name; Person(int a, string& n) : _age(a), _name(n) {} };

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

Whoosh Posted: Apr 17, 2005 2:51 PM
Reply to this message Reply
> > Templates are kind of a code generation hack to
> simulate
> > dynamic typing.
>
> Not at all, every template is instantiated at compile time
> and has a static type.

You completely missed my point. C++ programmers use templates to gain the same kinds of benefits that users of dynamically typed languages enjoy.

It gets you out of re-fiddling your inheritance hierarchy just to gain substitutability. Without templates, you must write a function in terms of an abstract interface that can be found to be formally declared within the argument's inheritance hierarchy.

class A { public: int foo() {}};
class B { public: int foo() {} };

void f(A& a) { a.foo(); }

I can only call f with an A even though B clearly implements the same protocol. That's classic C++ static typing. Inheritance implies protocol.

However, this turned out to be too restrictive and an obstacle to code reuse, so a hack to simulate dynamic typing was introduced - the template. With a template we can now substitute a B for an A.

template <class T> void f(T& t) { t.foo(); }

Typing is the very core of the original C++ experience. Yet it turns out that protocol compliance is much more interesting than typing and the rise of ever more elaborate template programming bears that out.

Todd Blanchard

Posts: 316
Nickname: tblanchard
Registered: May, 2003

Re: The Elephant in the Living Room Posted: Apr 17, 2005 2:57 PM
Reply to this message Reply
"I guess that just goes back to whether you like static typing. Of course, the "advantage" (if you are a static typing fan) of the C++ template approach is that you'll get compile-time feedback if you try to use the function with an object that doesn't support the methods used ("protocol?" -- avoiding the word "interface")."

Actually, Objective C will allow you to choose.

NSString* str = [NSString stringWithCString: "Hi there!"]];
[str foo]; // warning, NSString does not implement 'foo'.

id str = [NSString stringWithCString: "Hi there!"];
[str foo]; // no warning - all messages to object of type id are assumed valid

So you decide how much help you want.

indranil banerjee

Posts: 38
Nickname: indranil
Registered: Nov, 2004

Re: Whoosh Posted: Apr 18, 2005 3:15 PM
Reply to this message Reply
> > > Templates are kind of a code generation hack to
> > simulate
> > > dynamic typing.
> >
> > Not at all, every template is instantiated at compile
> time
> > and has a static type.
>
> You completely missed my point. C++ programmers use
> templates to gain the same kinds of benefits that users of
> dynamically typed languages enjoy.

Splat! Sorry I still dont see your point. As you mention in your other post, if a template cant be instatiated you get a compile time error.
A vector<int> is not a vector<string> and you can only put an int in the former.
This isnt just a detail, it's the fundamental difference between static and dynamic typing.
You can discuss which is better to have in a language. But it is a misrepresentation to say generic programming is a limited form of dynamic programming. They're different techniques to solve different problems.

For me the biggest benefit dynamic typing is that a dynamic type does not need to know at compile time how it is going to be used at runtime. Much lower coupling, much better encapsulation. With templates you have to know everything up front.

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: The Elephant in the Living Room Posted: May 2, 2005 2:31 PM
Reply to this message Reply
(Sorry for the late reply)

Keith Ray:

> "Templates allow you to write generic code (STL being one
> example), where the components are decoupled from each
> other, in a way you can't do with OO. An example:
>
> "template<class T> T some_operation(const T &value) {...}
>
> "This provides an operation for any type T (where it
> compiles;"
>
> You can do generic code in Smalltalk, Python, Ruby, etc.,
> without special syntax, without having to have a common
> base class, and without having to recompile.

Right, and that's because they use dynamic typing, so essentially variables are of type "variant".

However, C++ enables you to do this using static typing, so any type-related errors are caught at compile-time, and you get the efficiency of compile-time binding.

Regards,

Terje

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: Templates vs Dynamic Typing Posted: May 2, 2005 2:47 PM
Reply to this message Reply
Todd Blanchard:

> Templates are kind of a code generation hack to simulate
> dynamic typing.

I don't agree with that. You might just as well (and equally unjustifiably) say that dynamic typing is a "hack" to do what templates does.

Templates are a way to get parameterised types and functions. It's a natural feature for a statically typed language like C++ (and Java have finally come to the same... Designing a statically typed language without generics was not the brightest idea they had...). It enables you to keep static type checking and type safety (rather than loosing it to casts, as in pre-Java 5.0), and disband of using macros for that (which _could_ be described as a "hack").

With dynamic typing, on the other hand, you don't have the static type checking, so these are really not comparable, even though they solve similar problems, at different times (compile-time and run-time).

> template <class T> const T& max(const T& t1, const T& t2)
> {
> return t1 > t2 ? t1 : t2;
> }
> class A { int _x; public: A(int x) : _x(x){} };
> max(3,5) - works
> max(2.2,4.1) - works
> max(A(2),A(3)) - won't work
>
> why? Because A doesn't respond to protocol for >
> comparison. Templates give you a mechanism to code to the
> protocol of a receiver rather than its inheritance
> hierarchy like you have to with non-template code.
>
> Dynamically typed languages like Smalltalk work entirely
> on protocol rather than inheritance hierarchy. So
> template programming has much more in common with
> Smalltalk than with pre-template C++. Which I suspect is
> why C++ developers find it so useful. Its the best
> illustration of why C++'s original strongly checked type
> model based on inheritance was a mistake.

I don't agree on that, either. Again, these are examples of different kinds of polymorphism, as well as whether or not types are static or dynamic. (Inheritance-based) OO supports - like templates - compile-time type checking, so again, the run-time dynamical lookup version of Smalltalk etc. is not a substitute for it. They are different ways, and have different consequences.

Thus, I think calling templates a "code generation hack to simulate dynamic typing" is a wrong way of looking at things: They both offer different ways of doing it, with different consequences.

Regards,

Terje

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: Why? Posted: May 2, 2005 3:03 PM
Reply to this message Reply
Todd Blanchard:

> "I like that C++ doesn't enforce a particular programming
> style or paradigm on you, and offers several, as they are
> useful for different purposes."
>
> Yes, but its not particularly good at any of them.

Could you give some examples?

- When it comes to OO, for example, C++ supports access specifiers, multiple inheritance, virtual inheritance, virtual and non-virtual functions, access and inheritance being orthogonal (you can inherit and override a private function, but not call it), and so on. Comparing with other popular languages, they tend to be rather a lot simpler when it comes to support for OO, and hence the available expressivity.

- When it comes to support for generic programming, it again shines (I've written more about that in another posting, so I won't repeat it). I can just add equal or better language support for user-defined types, as for built-in types, including operator overloading, and that it treats built-in and user-defined types the same way (so there's no inherent performance penalty for user-defined types over built-in types, unlike Java). Again, can you point to another language that does it better, and why?

- Support for structured programming. C++ gives first-class support for free functions. Languages like Java don't do that.

- Support for functional programming. This is pretty decent, with library support, but this is still an area of exploration. Other languages, like Haskell, provide this natively (but then _don't_ provide other things that C++ does, such as iteration).

- Quite good support for DSL building (Boost.Spirit being one example). One big advantage with having a DSL in the language is that it interoperates seamlessly with the language.

> I
> prefer a nice scalpel for surgery over a swiss army knife.

The problem is when all you have is a scalpel, when doing surgery...

Naturally, C++ (like Java) is a general-purpose language, and there may well be cases where a more special-purpose language could be preferrable. Examples include DSLs like SQL, regex, HTML, etc.

Regards,

Terje

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: The Elephant in the Living Room Posted: May 2, 2005 3:11 PM
Reply to this message Reply
Matt Gerrans:

> Ah. Well I guess that just goes back to whether you like
> static typing. Of course, the "advantage" (if you are a
> static typing fan) of the C++ template approach is that
> you'll get compile-time feedback if you try to use the
> function with an object that doesn't support the methods
> used ("protocol?" -- avoiding the word "interface").
>
> Hmm... Are templates something different than shorthand
> for an undeclared interface? Maybe they imply an
> "Annonymous interface?"

As you imply, at the moment, there's not much one can do to enforce a specific interface in a class/function template declaration, that the types used to instantiate it with has to adhere to. One way in the current language is to use Boost Concept Checking Library, and another is to "play tricks" with boost::enable_if. However, what is really needed is support for "concepts" in C++ (constrained genericity, something Java 5.0 and C# has in some form), and as mentioned in another posting, there exists a couple of proposals for this, and it'll probably be in C++0x (althought it's also probably a little early to say, but there's strong interest for it, in the standards committee).

Regards,

Terje

Flat View: This topic has 38 replies on 3 pages [ « | 1  2  3 ]
Topic: Bolt-Ins for Contract and Extension Classes Previous Topic   Next Topic Topic: How to Draft a Pattern

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use