Representing dynamic typing in C

前端 未结 6 1553
时光说笑
时光说笑 2020-12-14 22:10

I\'m writing a dynamically-typed language. Currently, my objects are represented in this way:

struct Class { struct Class* class; struct Object* (*get)(stru         


        
6条回答
  •  [愿得一人]
    2020-12-14 22:40

    See Python PEP 3123 (http://www.python.org/dev/peps/pep-3123/) for how Python solves this problem using standard C. The Python solution can be directly applied to your problem. Essentially you want to do this:

    struct Object { struct Class* class; };
    struct Integer { struct Object object; int value; };
    struct String { struct Object object; size_t length; char* characters; };
    

    You can safely cast Integer* to Object*, and Object* to Integer* if you know that your object is an integer.

提交回复
热议问题