Hacker News new | past | comments | ask | show | jobs | submit login

>that there is no official mechanism for declaring types and interfaces.

This is not true for Common Lisp.

Want to declare types? "(declare (type ...))"

Interfaces? CLOS allows "generic functions", which can achieve the same as interfaces, and are light years beyond the typical OOP system.




I wasn't aware of Common Lisp type declarations, thanks. But they are optional (right?) so in practice if they are not there they do little good.

Could you tell a bit more about how generic functions do the same thing as Interfaces? Can you use them as part of a type-declaration? Say like a Java Interface can be used as a Type in a type-declaration of a method.


> But they are optional (right?) so in practice if they are not there they do little good.

That depends on the compiler. The SBCL compiler will use it as type assertions, adding type inference and will do checks at compile time.


>But they are optional (right?) so in practice if they are not there they do little good.

Modern implementations like SBCL also do type inference as well and do tell you about compile-time type errors.

CL is also a very strongly typed language so type errors are going to be caught by the implementation sooner or later.

Not that I care, though. Type mismatch errors are my least concern.

>Could you tell a bit more about how generic functions do the same thing as Interfaces?

You'll have to read the online chapter about Generic functions of the "Practical Common Lisp" book, available online for free and already a classic. Common Lisp Object System (CLOS) cannot be explained in a few words.

>Can you use them as part of a type-declaration?

Object classes can be used in type declarations.

>Say like a Java Interface can be used as a Type in a type-declaration of a method.

CLOS goes beyond and is even more powerful than what Java offers. See "multimethods" and "polymorphic dispatch".

CLOS OOP makes Java OOP look like looking at a Little Tikes car from an actual Formula 1 car. And I say this as a person who worked as a paid Java developer for "serious" stuff for years.


> (CLOS) cannot be explained in a few words.

No doubt. I was just curious how "generic functions" in particular could help in defining types and interfaces. I was not asking for the whole story about the whole of CLOS.


>I was just curious how "generic functions" in particular could help in defining types and interfaces. I was not asking for the whole story about the whole of CLOS.

I understand.

For something similar like a Java interface, you could define generic functions for each method your interface declares. For example consider interface IStream with methods open(), close(), sendbytes(b) and readbytes(b)

In Lisp you could declare the following generic functions: open(x), close(x), send(a, b), and read(a,b).

Then you simply define methods for any kind of (concrete) stream. Moreover, "send" and "read" could be defined to send/read not just bytes, but also any other kind of object. Or you could also define a "send" and "read" that works from stream to stream. For the latter, on java you would have to either redefine IStream, extend IStream, or create another class specialized on copying from Stream to Stream. On Lisp you just define an additional method for the generic function send(a,b) where a and b are both streams.

The CLOS system will take care of calling the correct method. If there isn't a correct method this would signal an error: "No applicable method".




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

Search: