3 ms·
Julia does a few things differently then Common Lisp, though they both offer multiple dispatch. One of the key things in CL is that it has its metaobject proto
by Jtsummers 5y ago
Julia does a few things differently then Common Lisp, though they both offer multiple dispatch.
One of the key things in CL is that it has its metaobject protocol which forces a lot of decisions on what gets executed to runtime. There are ways to speed it up, but if you have something like:
(defgeneric foo (x))
(defmethod foo ((x number)) (print 'number))
(defmethod foo ((x integer)) (print 'integer))
(defmethod foo :before ((x number)) (print 'also-a-number))
(foo 3)
(foo 3.0)
Then CL won't call foo specialized on number when given an integer, but will call foo :before specialized on number. It determines this at runtime by searching for all applicable methods based on the type (at least as a first pass, you can cache this to speed it up but then you also have to have cache invalidation if a definition is changed).
Julia doesn't have that aspect of CL's MOP. So this helps to simplify the search for applicable methods and dispatch. Even if it did all its dispatch at runtime, it would still be simpler. The other thing Julia does is aggressive JIT compilation. So if you wrote something like (with the Julia equivalent of foo from above):
function bar(x,y)
foo(x)
foo(y)
end
And, only considering floats and integers, later called it with each pair of float and integer then Julia would compile specialized versions for those 4 combinations. Now when you call bar it still has to properly dispatch it, but once inside bar the search for the correct foo can be bypassed because the types will be known. CL, again thanks to the MOP, doesn't make that as easy to achieve.