Implementing dynamic typing in C

Possible duplicate:
Representation of dynamic typing in C

The professor of my lecture, “History of Computing,” studied in detail the clearly typed or printed types of languages ​​and generally praised the greatness of language languages ​​(faster development time, dynamic systems, etc.).

On the same day, another professor says in Automata class:

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

I use statically typed languages ​​for most of my life: C / C ++ / Java - my only access to another is Shell Coding and Ren'Py.

My question is: can I write a simple C program that implements some of the advantages of both?

For example, I could create Unions to accept all user-driven data, for example:

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 understand that the Union may jeopardize type safety, but here's what I'm trying to do here. What other approach could I take? I'm just trying to demonstrate.

I tried to answer from this question . But honestly, I couldn’t follow the arguments exactly.

Sorry if the question seems silly.

PS

Using the suggestions below, I wrote the following: http://codepad.org/A9JAX8lD , which basically doesn't do anything dynamic, but at least is a start.
I seem to see what my professors tried to say.

+6
source share
3 answers

My suggestion is not to try to perform dynamic typing in a statically typed language. This will most likely have lower performance and strong syntax load. Instead, if you have only ever come across statically typed languages, I would strongly suggest trying Python . He is very dynamic and will teach you new ways of thinking.

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

+5
source

I am not against types, but I do not know any type systems that are not a complete pain [...]
- Alan Kay

It is possible to implement a fully functional dynamic type system on top of C: Take GType, on which the GLib object system is based.

However, such systems often become painful to use because of the amount of template code required, which can be handled using native code generators and preprocessors that start with Objective-C.

+1
source

If you want to show how C might be "unsafe by type", try using void * to pass arguments. The disadvantage is that it is not really dynamic, since you cannot call any methods on an object without dropping it first.

+1
source

Source: https://habr.com/ru/post/912306/


All Articles