Why am I able to call the class method as if it were an instance method here? - sql

I was looking through this example:
class SQLObject
def self.columns
return #columns if #columns
columns = DBConnection.execute2(<<-SQL).first
SELECT
"#{table_name}".*
FROM
"#{table_name}"
LIMIT
0
SQL
columns.map!(&:to_sym)
#columns = columns
end
def self.table_name
#table_name ||= self.name.underscore.pluralize
end
def insert
column_symbols = self.class.columns.drop(1)
column_names = column_symbols.map(&:to_s).join(", ")
question_marks = (['?'] * column_symbols.count).join(", ")
DBConnection.execute(<<-SQL, *attribute_values)
INSERT INTO
#{self.class.table_name} (#{column_names})
VALUES
(#{question_marks})
SQL
self.id = DBConnection.last_insert_row_id
end
end
And I was confused as to why it's ok to call the table_name method in the "self.columns" method as if it were an instance method. Isn't the "table_name" method a class method? Hence, shouldn't it be called as "self.class.table_name" in the "self.columns" method as well?

When inside an abstract class, the self refers to the proper class, not the object. That's why you can access the method without explicitly telling self

Short answer: self is the implicit receiver in Ruby. If you leave it out, the message will be sent to self. In other words, it is never necessary to say self.foo in a message send, because foo is always implicitly the same as self.foo.
Long answer: there is no such thing as a "class method" in Ruby. What you call a "class method" is actually nothing but a singleton method defined on an object which just happens to be an instance of the class Class.
Actually, there are no singleton methods in Ruby, either. A singleton method is really nothing but a regular old instance method which is defined on the singleton class of an object.
[Note: This is not to say that you should not use those terms. Saying "class method" is clearly simpler and communicates intent better than "instance method of the singleton class of an object of class Class". Also, note that there are methods such as Object#singleton_methods. Those terms clearly exist in the Ruby Community, but it is important to understand that these concepts do not exist in the Ruby Language. They are a tool for communication, not an actual Ruby concept.]
The singleton class is a class that is associated with an individual object. Each object has exactly one singleton class and that object is the only instance (the "singleton instance", hence the name) of its singleton class.
In fact, the inheritance hierarchy of an object always starts with its singleton class, i.e. the class pointer of an object always points to its singleton class, and the singleton class's superclass pointer then points to the class that created this object. You just don't see the singleton class in the inheritance chain, because methods like Object#class "skip over" the singleton class when calculating the inheritance chain. It is, however, always used during method lookup.
In fact, method lookup is actually very simple (the only exception is Module#prepend, which I am going to ignore for this explanation):
Dereference the receiver's class pointer.
If that class's method table has a method by the name of the message, invoke it. STOP.
If not, dereference the class's superclass pointer.
GOTO #2.
If you reach a class that doesn't have a superclass, restart the algorithm with the message method_missing(original_message_name, ...), unless the message name is already method_missing, in which case raise a NoMethodError.
So, it all comes down to two questions:
When defining a method, which class (or module) are you defining it in?
When sending a message, which object are you sending the message to?
When you define a method using def foo.bar, Ruby will first evaluate the expression foo and then define a method bar on the resulting object's singleton class (the definee). If you don't provide foo, i.e. you just say def bar, then bar is defined on the default definee, which is a little bit of a tricky concept. Normally, it is the closest lexically enclosing module definition. If there is no enclosing module definition, i.e. you are at the top-level, the default definee is Object and the method visibility is private.
When you send a message using foo.bar, Ruby will first evaluate the expression foo and then send the message bar to the resulting object (the receiver). If you don't provide foo, i.e. if you just say bar, then the default receiver is self.
Which brings us to the next question:
What is self?
Within a method definition body, self is the receiver of the message send that led to the invocation of this method. So, if you send the message foo.bar, then any reference to self in the definition of the method bar will, during this one execution of the method, evaluate to foo.
Within a module or class definition body, self is the class or method itself. (That is why defining a singleton method instance method of the singleton class using def self.bar works in a module or class definition body.)
Within a block or lambda literal, self is lexically captured, i.e. it is whatever self is at the point where the block or lambda literal is written. However, there are a couple of methods which change how self is evaluated, most notably the BasicObject#instance_eval, BasicObject#instance_exec, Module#module_eval, Module#module_exec, Module#class_eval, Module#class_exec, … family of methods.
If you are always aware of the answers to those three questions (where am I defining a method, what is the receiver of a method call, what is self) at any point in your code, then you have basically understood the most important parts of Ruby.
So, to put it all together:
At the point where you are defining the method using def self.columns, we are in a class definition body, so self refers to the class object itself (SQLObject).
The syntax def foo.bar defines a method bar on the singleton class of foo. In this case, you define the method columns on the singleton class of SQLObject. This means that the object SQLObject is the only object in the universe that will respond to the message columns.
Both of these also apply to the definition of self.table_name.
Inside of the method body of self.columns, self will dynamically refer to whatever the receiver is.
When you send the message columns to SQLObject, i.e. you write SQLObject.columns, then inside the body of the SQLObject::columns method, the receiver (i.e. self) will be set to SQLObject. (In this case, the receiver will always be SQLObject, since this is a method of a singleton class.)
So, inside the method body of self.columns, when you write the message send table_name, this is a message send with an implicit receiver. Since the implicit receiver is self, this is equivalent to self.table_name. self at this point is bound to SQLObject, so this is equivalent to SQLObject.table_name. In other words, the message is sent to SQLObject.
Now, per the algorithm I outlined above, the method lookup first retrieves the class pointer from SQLObject. As mentioned, the class pointer always points to the singleton class. So, we look inside the singleton class of SQLObject to see if we find a method named table_name, and we do!
Method lookup is finished, everything works.

Related

Where are class variables defined in Smalltalk?

I was wondering, if I define a new class variable, for example for the class MyClass, is the definition going to be in MyClass or in MyClass class? Does MyClass class even know about the new class variable?
Yes, class variables are shared with the class and the metaclass. They are also shared with all subclasses (and their metaclasses). A class variable is usually Capitalized, to convey better the idea of being shared in a scope broader than the class. You define class variables in the class (not the metaclass).
Class variables should not be confused with class instance variables, which are instance variables defined at the metaclass level, i.e., instance variables of the class object. This notion is somewhat obscure despite its simplicity (or because of it): instance variables are always defined in the class to define the shape (slots) of its instances. Thus, if we apply this definition to the metaclass, which is the class of the class, an instance variable defined here defines the shape of its instances, of which there is (usually) only one, the class.
Going back to class variables, you define them in the class (inst side) and initialize them in the metaclass (i.e., class side). Remember that these are (partial) globals in the sense that will be shared among instances, subinstances, subclasses and metaclasses and so they must be handled with the usual care we treat globals.
One more clarification
When we say that instance variables are shared among instances and subinstances, we mean their names (and positions in memory of the object slots); we don't mean their values (contents of said slots). Thus, two instances of the class C will share the name, say color, if the class defines the ivar color, but their values at each of the instances will be independent. In other words, what it is shared is the name, not the value.
With class variables what is shared is both the name and the value. It is actually the Association object, for example Theme -> aTheme, what's shared. In consequence, any modification to the value of a class variable affects all its references. This is not the case with class instance variables because they are nothing but instance variables, except that they shape the class and its subclasses, rather than regular instances and subinstances.
For more information on Smalltalk variables see https://stackoverflow.com/a/42460583/4081336
Just as a complement to Leandro's answer, here is the main Squeak implementation specific method that explains the sharing of class variables between instance side (class) and class side (metaclass):
Metaclass>>classPool
"Answer the dictionary of class variables."
^thisClass classPool
where thisClassis the unique instance of the Metaclass, that is the class itself...
There are high chances though to find similar implementation in most Smalltalk dialects.
The compiler will first try to resolve the variable as a method/block temporary (including methd/block parameters), then instance variables, then shared variables.
The classPool method is sent by the compiler in this last phase.
A Leandro did explain, the compiler either resolve the binding just as an offset that will be directly transcripted in the bytecode in case of instance variable slot or method temporary variable, or as a kind of Association for the shared variable case, this association being generally added to the CompiledMethod literals and effectively shared among all methods dealing with this variable (all methods points to the same Assocation object which is effectively shared).
The compiler part is much more dialect specific, in Squeak, it's this method which is used for resolving the binding of shared variables:
class>>bindingOf: varName environment: anEnvironment
"Answer the binding of some variable resolved in the scope of the receiver"
| aSymbol binding |
aSymbol := varName asSymbol.
"First look in local classVar dictionary."
binding := self classPool bindingOf: aSymbol.
binding ifNotNil:[^binding].
"Next look in local shared pools."
self sharedPools do:[:pool |
binding := pool bindingOf: aSymbol.
binding ifNotNil:[^binding].
].
"Next look into superclass pools"
superclass ifNotNil: [^ superclass bindingOf: aSymbol environment: anEnvironment].
"No more superclass... Last look in declared environment."
^anEnvironment bindingOf: aSymbol
This is to remind you that one of the most interesting part of Smalltalk is that you can dig into the implementation from within the IDE, Smalltalk is essentially written in Smalltalk!

How should super behave with instance-specific behavior?

Assume object is an instance of class C. Also assume that object has an instance-specific method m attached to it. The method m is defined in both C and its superclass C0. The question is, which method should the expression
super m
invoke when self == object, and why?
I see two possible answers:
C >> #m (the method in the object class)
C0 >> #m (the method in the object class superclass)
EDIT
Even though the way we implement instance-specific behavior shouldn't matter for the semantics of super, let me point out that my favorite implementation is the one that places the so called MethodDictionaryArray (or MDA for short) in object headers, instead of the object class. As you can imagine, a MDA contains the method dictionaries of the inheritance chain.
With this implementation you can put instance behavior in a new MethodDictionary (MD) and redefine the object's MDA as the nested array #{MD. MDA}.
IMHO it should invoke C0>>m to behave just like 'normal' instances of C. The implementation detail of how the instance-specific behavior is realized shouldn't matter. If you copy a method from C to its instance, it should ideally behave exactly the same as before.
The standard definition of super is that super sends a message to the same receiver as self but starts the method lookup in the class above the one where the current method is defined.
It seems to me that one can phrase the definition a bit differently,
to get at the proper resolution of the semantics.
If we consider the normal case to be equivalent to
'do not include in the search any methods that are defined
by the receiver, include only those which are inherited'
then the implementation details are not required when we are
deciding which answer to expect.

Init a class with one argument using objc_msgSend [duplicate]

This question already has answers here:
Create a subclass of a class using parent's init - from another class
(2 answers)
Closed 8 years ago.
EDIT: Yes, I did it wrong. It's well possibly knowing the init method by using a protocol on class level. This is something I rarely do, so that didn't come to my mind at first (see linked question about my answer to it using a protocol). So yes, this question is broken. As bbum said, there should be absolutely no reason to do this.
Background to my question in [1].
For a design reason (data mapper pattern) I need to initialize classes which I know are subclasses of a certain base class (ManagedEntity). I assert for this once - then later I want to create as many instances, and as fast as possible (I'm programming for iOS). However, since the class where I need to create the concrete instances in doesn't know any of the model classes, the meta class stored and used to create entity instances of is just known to be of type Class.
Long story short: I can't simply use [[[_EntityClass] alloc] initWithBlah:something], since EntityClass is unknown, just known as type Class there, hence the init method initWithBlah is unknown of course - but I know it must exist (it must be by design a subclass of the base class, which is asserted once when the mapper is initialized).
So in order to create instances of the unknown class with the init method that I know it exists, I need to construct a method invocation. This should call the initWith:something selector on the unknown class and create an instance of it.
I think I should use objc_msgSend rather than NSInvocation, because the latter is supposed to be an order of magnitude slower [2]. The init method is supposed to not change, and requires one argument.
So... What would be the equivalent to:
ManagedEntity *newEntity = [[ManagedEntity] alloc] initWithEntityDescription:_entityDescription];
with objc_msgSend?
[1] Create a subclass of a class using parent's init - from another class
[2] http://www.mikeash.com/pyblog/performance-comparisons-of-common-operations-leopard-edition.html
Better:
Class klass = NSClassFromString(className);
id newEntity = [[klass alloc] initWithEntity:entity insertIntoManagedObjectContext:ctx];
There is no reason to use objc_msgSend() directly when you have a fixed selector. You can always call the selector directly using the normal syntax. Worst case, you might have to type-cast the return value of one of the calls.
The only requirement is that the compiler has seen the declaration of initWithEntity:insertIntoManagedObjectContext: sometime prior to compiling the above call site.
Example:
#interface NSObject(BobsYourUncle)
- (void)bob:sender;
#end
...
Class klass = NSClassFromString(#"NSManagedObject");
[[klass alloc] bob:nil];
The above compiles just fine. Not that I'd recommend hanging random definitions off of NSObject. Instead, #import the abstract superclass's declaration (which should contain the selector declaration).
id cls = NSClassFromString(className);
id alloced_cls = objc_msgSend(cls, #selector(alloc));
id newEntity = objc_msgSend(alloced_cls, #selector(initWithEntity:insertIntoManagedObjectContext:), entity, ctx);
return newEntity;

Objective-C Selector pointer to be passed to a C function

I have a C struct that contains a function pointer. Now, I have used this setup within C with no problems, but now I'm using this C struct in Objective-C and I need to pass a function (or selector) pointer that is defined in the Objective-C class.
1. Here is what I have for the Objective-C selector that needs to be passed as a pointer to the C function:
- (void)myObjCSelector:(int*)myIntArray
{
// Do whatever I need with myIntArray
}
2. And here is where I run into a wall, Within Objective-C I'm trying to pass the selector as a pointer to the C function call: In place of "myObjCSelectorPointer" I need the proper syntax to pass the selector as a function pointer in this C function call:
passObjCSelectorPointerToCContext(cContextReference, myObjCSelectorPointer);
I did investigate this issue, but could mainly find several different ways of doing similar things, but I couldn't find anything specific for calling C functions and passing an Objective-C selector pointer.
In objc a selector is not a function pointer. A selector is a unique integer that is mapped to a string in a method lookup table stored by the objc runtime. In the above case your method name would be myObjCSelector: and to get the unique selector for it you would type #selector(myObjCSelector:). However this would be of no use to you because it doesnt represent a particular implementation of a function.
What youre looking for is IMP. Refer to this SO question.
EDIT 2:
IMP myObjCSelectorPointer = (void (*)(id,SEL,int*))[self methodForSelector:#selector(myObjCSelector:)];
Then you can call the method using
myObjCSelectorPointer(self,#selector(myObjCSelector:),myIntArray);
However, what this means you will need to make sure that you add the pointer to self in the c function call passObjCSelectorPointerToCContext.
So it should look like this
passObjCSelectorPointerToCContext(cContextReference, self, myObjCSelectorPointer);
when called from within the object that contains the method.
It is important to note though that using IMP is almost never the right technique. You should try to stick with pure Obj-C. Obj-C is quite efficient after the first call to a message because it uses temporal caching.
EDIT 1:
It's useful to understand why objc works in this way. The Apple documents explain it in depth. However a short explanation is as follows:
When you send a message to an object such as [myobject somemethod] the compiler won't immediately know which particular implementation of somemethod to call because there might be multiple classes with multiple overriden versions of somemethod. All of those methods have the same selector, irrespective of its arguments and return values and hence the decision about which implementation of somemethod is deffered to when the program is running. [myobject somemethod] gets converted by the compiler into a C function call:
objc_msgSend(myobject, #selector(somemethod))
This is a special function that searches each myobject class layout to see whether that class knows how to respond to a somemethod message. If not it then searches that class's parent and so on until the root. If none of the classes can respond to somemethod then NSObject defines a private method called forward where all unknown messages are sent.
Assuming that a class can respond to the somemethod message then it will also have a particular pointer of type IMP that points to the actual implementation of the method. At that point the method will be called.
There is considerably more to this procedure than I have described but the outline should be enough to help you understand what the goal of a selector is.
One final point is that the reason method names are mapped to unique integers via the #selector directive is so that the runtime doesn't have to waste time doing string comparisons.
Basically, the answer is: Objective-C selectors are different from function pointers. You need two pieces of data to perform a selector. That is an object and the selector itself. You will need some glue to accomplish your task.
Check this question.
Do you have to use a function pointer? In Objective-C, you can get the function pointer to an arbitrary method implementation (known as an IMP), but this is extremely uncommon, and usually not a good idea. Calling objc_msgSend() directly is also not the greatest idea, because there are several different variants of objc_msgSend(), and the compiler automatically chooses different ones to use based on the return type of the method. Methods that return an object go through objc_msgSend(), but objects that return structs might go through objc_msgSend() or they might go through objc_msgSend_stret(). And if the method returns a double, then it goes through objc_msgSend_fpret()...
Documentation: Objective-C Runtime Reference: Sending Messages
Instead, I might recommend using a target-action pair, or using a block. Then you might do something like:
myContextRef->target = anObjcObject;
myContextRef->action = #selector(invokeMe:);
And when you're done, do:
[myContextRef->target performSelector:myContextRef->action withObject:someReturnInformation];
Or maybe use a block:
myContextRef->completionHandler = [^(id returnInformation) {
[anObjcObject invokeMe:returnInformation];
} copy];
And then when you're done, do:
myContextRef->completionHandler(someReturnInformation);
(and don't forget to -release the block when you free the context)

What's the difference between declaring a variable "id" and "NSObject *"?

In Objective-C, what's the difference between declaring a variable id versus declaring it NSObject *?
With a variable typed id, you can send it any known message and the compiler will not complain. With a variable typed NSObject *, you can only send it messages declared by NSObject (not methods of any subclass) or else it will generate a warning. In general, id is what you want.
Further explanation: All objects are essentially of type id. The point of declaring a static type is to tell the compiler, "Assume that this object is a member of this class." So if you send it a message that the class doesn't declare, the compiler can tell you, "Wait, that object isn't supposed to get that message!" Also, if two classes have methods with the same name but different signatures (that is, argument or return types), it can guess which method you mean by the class you've declared for the variable. If it's declared as id, the compiler will just throw its hands up and tell you, "OK, I don't have enough information here. I'm picking a method signature at random." (This generally won't be helped by declaring NSObject*, though. Usually the conflict is between two more specific classes.)
id means "an object", NSObject * means "an instance of NSObject or one of its subclasses". There are objects in Objective-C which are not NSObjects (the ones you'll meet in Cocoa at the moment are NSProxy, Protocol and Class). If some code expects an object of a particular class, declaring that helps the compiler check that you're using it properly. If you really can take "any object" - for instance you are declaring a delegate and will test all method sends with respondsToSelector: calls - you can use an id.
Another way to declare an object variable is like "id <NSObject>", which means "any object which implements the NSObject protocol.
From my limited understanding of Objective-C, not all objects are derived from NSObject (unlike Java where all objects derive from Object). You can theoretically have other root objects. id could apply to any of those non-NSObject derived objects.
I would like to add another difference. When you add a protocol to id, it does not longer mean that it will be of type NSObject *, it just means that it will be any class that confirms to that protocol.
So, for example, this code will not throw any error, since NSObject's category NSDelayedPerforming has that method:
id testId;
[testId performSelector:#selector(isKindOfClass:) withObject:[NSObject class] afterDelay:.5];
However, this code will show the error No known instance method for selector "performSelector:withObject:afterDelay:":
id<NSMutableCopying> testId;
[testId performSelector:#selector(isKindOfClass:) withObject:[NSObject class] afterDelay:.5];