Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Are you saying instead of this:

  def method():
    this.value = this.value + 1
lisp does something like this?

  def method(object):
      object.data = object.data + 1


Yes. But while this is single dispatch. The real clue is that you can have multiple dispatch.

So you might have.

define generic inspect-vehicle (v :: <vehicle>, i :: <inspector>) => ();

define method inspect-vehicle (v :: <vehicle>, i :: <inspector>) => () look-for-rust(v); end;

define method inspect-vehicle (car :: <car>, i :: <inspector>) => () next-method(); // perform vehicle inspection check-seat-belts(car); end;

define method inspect-vehicle (truck :: <truck>, i :: <inspector>) => () next-method(); // perform vehicle inspection check-cargo-attachments(truck); end;

This is Dylan code, but its the same concept, easier to read compared to CL. See: https://opendylan.org/documentation/intro-dylan/multiple-dis...

Once you start thinking about dispatch in a broader sense the advantage is clear.


Yes. Note that `this` is just syntactic sugar; a call to `obj.method(arg)` is, underneath, essentially a call to `method(obj, arg)`, with the first argument always being hidden, and the only one that's a subject of a method dispatch (polymorphism).

Common Lisp gets rid of implicit `this` by making all arguments explicit, and by not restricting polymorphism to the first parameter - in fact, you can do a method dispatch on any of the method parameters, or any combination of them. See https://news.ycombinator.com/item?id=18848384 for an example.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: