The Artima Developer Community
Sponsored Link

Weblogs Forum
Bridging Static and Dynamic Typing

1 reply on 1 page. Most recent reply: Apr 17, 2006 5:14 AM by Achilleas Margaritis

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 1 reply on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Bridging Static and Dynamic Typing (View in Weblogs)
Posted: Apr 13, 2006 6:59 AM
Reply to this message Reply
Summary
Michael Feathers recently suggested that maybe static typing is a form of bad coupling which we could do without. Well in C++ at least you can bridge static typing and dynamic typing.
Advertisement
Okay I lured you in, but I'll have to admit what I am about to propose here isn't elegant. C++ support for both generic programming and dynamically typed programming is extremely rudimentary, however I think there is something to take away.

The bridge I am talking about is as follows:

  template<typename T = ootl::object>
  class priority_queue {
     void push(T value, int priority);
     T pop();
     bool is_empty();
  };
In this example, ootl::object is my dynamically typed variant class which is available at http://www.ootl.org but you can just as easily use boost::any (which is slower, but perhaps a bit safer).

So what do you think? Dynamically typed, or statically typed, its your choice when you use templates.


Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Bridging Static and Dynamic Typing Posted: Apr 17, 2006 5:14 AM
Reply to this message Reply
Coding a Variant class in C++ is no problem at all. Unfortunately, C++ does not offer 'variant' semantics, so it takes more code to use a 'variant' instance properly. If a variant is used in expressions, then operators can be passed in the underlying object as messages (i.e. virtual methods). But there is no way to call a method of an object of a variant instance without casting the variant to a type, which makes the whole issue of using 'variant' more of a problem than a solution.

In other words, one can happily do in C++ the following:


var x = 5;
x += 1;
cout << x;
var y = x;
y = "the quick brown fox";


But the following is not possible:


var x = new foo;
x->bar();


unless one does this:


foo *f = (foo *)x;
f->bar();


which makes using 'var' reduntant, since we are going to type 'foo' anyway. Not only that, but it makes it very dangerous, because if I change 'foo' to 'bar' and then I forgot to update references to 'foo' in other places, then my program will certainly crash.

Flat View: This topic has 1 reply on 1 page
Topic: It's time for a new Java I/O API Previous Topic   Next Topic Topic: Shuttleworth Foundation Workshop on Analytical Skills Development

Sponsored Links



Google
  Web Artima.com   

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