On 19 May 2013 01:22, Ciprian Teodorov <[email protected]> wrote: > > > ---------- Forwarded message ---------- > From: Ciprian Teodorov <[email protected]> > Date: Sat, May 18, 2013 at 10:11 PM > Subject: Re: [Pharo-project] [NativeBoost] Allow hierarchy of objects in > function definition > To: [email protected] > > > Hi guys, > > Here is another idea which I think is nice, without disabling the > typechecker or anything: > > I suppose the LGitObjectExternal is a subclass of NBExternalObject > in LGitObjectExternal implement the #git_object_lookup as follows: > > LGitObjectExternal>primitiveLookupIn: repo id: id type: type > <primitive: 'primitiveNativeCall' module: 'NativeBoostPlugin' error: > errorCode> > ^self call: #(git_return_t git_object_lookup(self, git_repository_ptr > repo, git_oid_ptr id, git_otype type)) > > then all the calls to #primitiveLookupIn:id:type: would succeed in all > subclassed of LGitObjectExternal without doing anything special. >
You mean using "self" special argument. Yes. I like this construct a lot , it allow to avoid unnecessary type checks (since we know the class of receiver, we know it's layout and we can make much more assumption about it than an object passed as a method argument or from somewhere else). The marshaller of NBExternalObjectType actually checks for that case and do not generates code for type check, since type is known: the receiver will be either instance of class with this method or one of its subclasses. Since in our smalltalk implementation, slots reserved for instances in class is invariant for all of its subclasses, there is no danger in accessing instance variable (handle in our case) by its index, for any (sub)instance of the class. Unfortunately, you still need a check when you need to put method into other class which takes a (sub)instance of your external object as argument. For such cases, 'self' special argument in function signature apparently does not apply. > cheers, > ciprian > > > On Sun, May 5, 2013 at 12:14 PM, Max Leske <[email protected]> wrote: >> >> I have a wish for NativeBoost (which is probably on your list anyway Igor, >> just wanted to put it out there): function definitions should be able to >> understand that I want to accept any object of a hierarchy. >> >> Here's my use case: >> >> self call: #(git_return_t git_object_lookup(LGitCommitExternal * >> object, git_repository_ptr repo, git_oid_ptr id, git_otype type)) >> >> This function takes a pointer to any of LGitCommitExternal, >> LGitTreeExternal and LGitBlobExternal. The obvious way for me to do this was >> to create a superclass LGitObjectExternal and use that superclass in the >> function like so: >> >> self call: #(git_return_t git_object_lookup(LGitObjectExternal * >> object, git_repository_ptr repo, git_oid_ptr id, git_otype type)) >> >> But that gives me an error. So for now I can either use a dummy object >> (like NBExternalObject) and then convert from that or I have to implement >> the same function thrice in the different object flavors. >> Being able to use a superclass would just be awesome! >> >> Cheers, >> Max > > > > > -- > Dr. Ciprian TEODOROV > Ingénieur Développement CAO > > tél : 06 08 54 73 48 > mail : [email protected] > www.teodorov.ro > > > > -- > Dr. Ciprian TEODOROV > Ingénieur Développement CAO > > tél : 06 08 54 73 48 > mail : [email protected] > www.teodorov.ro -- Best regards, Igor Stasenko.
