New feature in Bike: member_missing
I always like the ability of objects to dynamically respond to a message without having an explicit binding. Something like method_missing in Ruby or DynamicObject in .NET. It would be great for Bike to have that capability. Fortunately, adding this feature was much easier than I thought, thanks to how the interpreter code was already laid out. Please join me welcoming member_missing in Bike! Let’s examine this feature more closely, shall we?
var obj = {
member_missing: func(name) {
return "Member: {0}".with(name);
}
};
println(obj.notExist); # Member: notExist
println(obj.has_member('notExist')); # False
The code above creates an object and defines the special member_missing method, which will be invoked by the runtime whenever it can’t resolve a member. Therefore, when trying to access notExist, methodMissing is invoked with the member name as its only argument and has a chance to decide what to return. In this case, it returns a string showing what member was being accessed.
This works for method invocation as well. When the interpreter attempts to invoke a method, the first thing it does is resolving the method object (Bike.Function) before invoking it. Therefore, member_missing can also do its magic here by returning a method that the runtime will instead invoke.
obj = {
member_missing: func(name) {
if (name == "add") {
return func(a, b) { a + b };
}
}
};
println(obj.add(1, 2)); # 3
You see, Bike’s member_missing doesn’t behave exactly like Ruby’s method_missing. I could modify the runtime to instead of invoking the function returned by member_missing, it will invoke something like method_missing (Bike edition!) and pass in the call information (e.g. name, arguments). Maybe that what I should have done for efficiency sake (i.e. avoid creating a function every time!). Then again, maybe later, one thing at a time, eh. For now, those who care about a bit of efficiency here and there, you can cache the generated method, as follow.
obj = {
member_missing: func(name) {
if (name == "cache") {
return this.cache = func() {};
}
}
};
obj.cache();
println(obj.has_member('cache')); # True
The next time obj.cache() is invoked, member_missing() isn’t invoke anymore because an explicit binding already exists.
Now, a more interesting example demonstrating the power of member_missing. If this somehow reminds you of Rails‘ ActiveRecord, you’re right.
var db = {
member_missing: func(name) {
if (name.starts('find')) {
return db[name] #! cache it !# = func(obj) {
var sql = 'SELECT * FROM {0}'.with(name.sub(4).upper());
obj.members(false, true).each_with_index(func(name, index) {
sql += (index == 0 ? ' WHERE ' : ' AND ') +
name.upper() +
(obj[name] is Bike.String ? '' : '=') +
obj[name]
});
println('Executing...{0}{1}', NL, sql);
return [#! Suppose I'm a product array, yay! !#];
};
}
}
};
# SELECT * FROM PRODUCTS WHERE CATEGORYID=1 AND PRICE>1000
var products = db.findProducts({categoryId: 1, price: '>1000'});
Interested? Have Bike up and running and start hacking. The examples above can be found in GitHub.





By PDGACO payday loans