{% puts Class.ancestors %}
outputs
[Value, Object]
Class should be reference, not Value, and I look source code
class Class
it is class Class, not struct Class.
so why Class superclass is Value?
I tried search but not found result.
{% puts Class.ancestors %}
outputs
[Value, Object]
Class should be reference, not Value, and I look source code
class Class
it is class Class, not struct Class.
so why Class superclass is Value?
I tried search but not found result.
It’s one of those weird things about bootstrapping the runtime that gets confusing. It’s like how in Ruby, Object
is an instance of the Class
class, which is a subclass of the Module
class, all of which are objects, which are instances of the Object
class. They’re categorized because they have to be but that categorization isn’t internally consistent because of the chicken-and-egg problem.
In practice, I would be surprised if you ever truly needed to know the type of Class
. I’ve written a lot of Crystal code in the past 6-7 years and I’ve never thought about what type it was until this thread.
Ha,as you mentioned I found Module is missing.
{% puts Class.ancestors %}
{% puts Module.ancestors %}
puts Class.is_a? Class
puts Class.is_a? Module
puts Module.is_a? Module
because I want to contribute to crystal core,
so I need to understand as deep as it is.
currently I’m reading (About this guide - Crystal)
page by page,
but I’m afraid most people don’t need read so detailed.
I guess, Any classes
, e.g. Object, Value, Reference, include Class
itself, as a object, is a Struct instance. [Value, Object]
is the super class of Struct. so the Struct can be created directly by language, then all classes(Include Value) create use Struct, then, set Value as superclass of Struct … well, i confused myself.
void
Init_class_hierarchy(void)
{
rb_cBasicObject = boot_defclass("BasicObject", 0);
rb_cObject = boot_defclass("Object", rb_cBasicObject);
rb_gc_register_mark_object(rb_cObject);
/* resolve class name ASAP for order-independence */
rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
rb_cModule = boot_defclass("Module", rb_cObject);
rb_cClass = boot_defclass("Class", rb_cModule);
rb_cRefinement = boot_defclass("Refinement", rb_cModule);
rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
RBASIC_SET_CLASS(rb_cClass, rb_cClass);
RBASIC_SET_CLASS(rb_cModule, rb_cClass);
RBASIC_SET_CLASS(rb_cObject, rb_cClass);
RBASIC_SET_CLASS(rb_cRefinement, rb_cClass);
RBASIC_SET_CLASS(rb_cBasicObject, rb_cClass);
ENSURE_EIGENCLASS(rb_cRefinement);
}
this is ruby code for this, crystal could also do sth like this
to set things up, but I guess they never bother to do.
what’s your problem? Every difference cause a fracture.
also if you don’t care these threads, then just don’t read these threads.
we have different concerns.
also I post here because there’s no core-discussion channel.
Classes themselves do not physically hold mutable state, that’s why they are allocated on the stack rather than the heap. (Class variables are effectively global variables but scoped, so there is no need for them to be physically attached to some class objects the same way instance variables are attached to normal objects.) This requires Class
to inherit from Value
rather than Reference
.
Ruby’s metaclass system is not the only possible realization of metaclasses, e.g. Objective-C does it differently. Crystal has no obligation to follow any.
What do you mean by Objective-C does it differently?
Here are the key points about how Objective-C implements its metaclass system:
Every Class Has a Metaclass:
In Objective-C, just like every instance of a class has a class, every class has a metaclass. This metaclass is responsible for holding information about the class itself, such as its methods and class methods.Metaclasses are Classes:
Metaclasses in Objective-C are themselves instances of a metaclass. This creates a hierarchy of classes and metaclasses. The chain starts with the root metaclass, and each subsequent metaclass is an instance of the previous metaclass.Metaclasses Store Class Methods:
While a regular class (an instance of its metaclass) stores information about its instance methods, the metaclass itself stores information about the class methods. This includes the class’s own methods and inherited methods from its superclass.Inheritance in Metaclasses:
Metaclasses follow the same inheritance structure as classes. If a class inherits from another class, its metaclass inherits from the metaclass of the superclass. This ensures that class methods are inherited in a manner similar to instance methods.
It seemed Objective-C’s metaclass system is the same as ruby’s?
perhaps very very minor difference between ruby’s, but basically it’s
almost the same as ruby’s metaclass system.
Thank you if you can clarify this:)
EDIT: it seemed obj-c metaclass has is_a? relation in them, while ruby metaclass doesn’t
say class Y < X;end (Y) denotes Y’s metaclass. so in ruby (Y)'s super is (X),
while in obj-c, (Y)'s super is (X) and also (Y) is_a? (X) .
Is this the difference? They still look nearly the same and sets up nearly the same
metaclass system.