10

Possible Duplicate:
Representing dynamic typing in C

A professor at my History of Computation side-lecture went into great depth about manifestly typed or type-inferred languages and generally praised the greatness of latently typed languages (faster dev times, dynamic systems, etc...).

The same day, at an Automata Class, another prof says:

Dynamic typing makes things more complex by adding more ways to do the same thing.

I've been using statically typed languages most of my life : C/C++/Java - my only exposure to the other has been Shell Coding and Ren'Py.

My question is, can I write a simple program in C that implements some of the benefits of both ?

For instance, I could create Unions to accept all user driven data, like so :

    typedef union {
        int int_type;
        char char_type;
        //and so on
    } dynamic;

   // Var Creation :
   dynamic data;

   // For unknown return type
   void* function(dynamic data);

I realize a Union could compromise type-safety, but that is what I'm trying to do here. What other approach could I take ? I'm just trying for a demonstration.

I tried for an answer from this question. But honestly, I could not follow the arguments closely.

I apologize if the question seems silly.

PS

Using suggestions from below, I wrote this : http://codepad.org/A9JAX8lD, which basically does nothing much dynamic, but is at least a start.
I think I see what both my professors were trying to say.

Community
  • 1
  • 1
RaunakS
  • 508
  • 1
  • 10
  • 20

3 Answers3

5

My suggestion is not to try doing dynamic typing in a statically typed language. It will most likely have sub-par performance and a very strong syntactical burden. Instead, if you only ever have experienced statically typed languages, I would strongly suggest trying out Python. It is highly dynamic and will teach you new ways of thinking.

And last but not least, there also is Cython which is a Python dialect using C as intermediate language. It can mix static typing and dynamic typing, it's really refreshing.

Socrates
  • 335
  • 2
  • 3
  • 11
orlp
  • 112,504
  • 36
  • 218
  • 315
  • I have a bare minimum of python experience while working on projects in [Ren'Py](http://www.renpy.org), though that probably doesn't count. And I will definitely check out Cython and it's source. – RaunakS Apr 03 '12 at 10:12
  • @nightcracker, ah yes, my apologies :) (I agree about cython, it's very cool!) – huon Apr 03 '12 at 10:17
1

I'm not against types, but I don't know of any type systems that aren't a complete pain [...]
-- Alan Kay

It's quite possible to implement a fully-featured dynamic type system on top of C: Take GType, on which the GLib Object System is based.

However, such systems are often painful to use because of the amount of boilerplate code they need, which can be worked around by using custom code generators and preprocessors, which is how Objective-C got started.

Christoph
  • 164,997
  • 36
  • 182
  • 240
  • Many thanks for pointing me to Gtype : I'm going through their [GNOME PAGE](http://developer.gnome.org/gobject/stable/gobject-Type-Information.html#G-TYPE-FUNDAMENTAL:CAPS) and it really is interesting. I'm looking at the SmallTalk wiki too - seems curious. – RaunakS Apr 03 '12 at 12:07
1

If you want to show how C can be "not type safe" try using void* to pass arguments. The downside is that it's not truly dynamic since you cannot call any methods on the object without casting it first.

Dror Helper
  • 30,292
  • 15
  • 80
  • 129
  • Could I use this, for instance ? http://codepad.org/m9hd38OV I agree, it's inefficient and doesn't do anything, but I could probably parse out something useful if required. – RaunakS Apr 03 '12 at 11:49
  • I think it's an interesting experiment - let me know the result – Dror Helper Apr 03 '12 at 20:30
  • Thanks for the ideas. [This](http://codepad.org/A9JAX8lD) is what I managed to scrape together - my function pointer capabilities are shallow. I'm giving up - people have done it much better in GType and Smalltalk anyway. – RaunakS Apr 03 '12 at 21:46
  • You never need to cast an argument to (void*) as long as it's a pointer of some type. It is done automatically. – Klas. S May 02 '17 at 02:23