20101005

NEW post

Hello,

here is a NEW post after a long time. And the topic of this post is NEW. Of course, this is old stuff, but here it is: it's a post about the new operator. I'd bet there are many other little hacks like this floating around, but I finally got sucked in into writing a little library that introduces a new operator (and a newq one) in Common Lisp.

Why? After a very long time of putting up with make-instance and make-this and make-that, and after many other brain things going on in my head (cit. King Julien of the Lemurs) I decided that Common Lisp needed something capable of doing the following:

cl-prompt> (defclass foo () ())
FOO
cl-prompt> (new 'foo)
#<FOO instance 42>

or even

cl-prompt> (newq foo)
#<FOO instance 42>

So; what's the big deal? Not much, but I also want to be able to write:

cl-prompt> (newq (array single-float (2 2)) '((1.0 0.0) (0.0 1.0)))
#2A((1.0 0.0) (0.0 1.0))

The problem is that as soon as you wade in CL "type specifiers" land, you hit the dreaded "implementation dependent" wall, or, worse, the "this is not even mentioned in the ANSI spec" wall. Think of DEFSTRUCT and the fact that there is no portable way to get to the constructor, not to mention DEFTYPE.

Apart from that, I think that the little piece of code I wrote would maybe make programming in CL feel more in tune with the times. After all you can also write things like the following, which I think are rather cool.

cl-prompt> (new '(mod 42) (read))
1024
Error: 1024 is not of type (INTEGER 0 (42)).
...

What do you think? Is this worth a CDR? Ping me if you want to see the code.


(cheers)

5 comments:

  1. Why don't you put it on a github repo? Isn't the theme of post to be in sync with the times.

    ReplyDelete
  2. I think it's a great idea - would simplify code for me. In my opinion (not sure if it counts much), it's worth a CDR :).

    ReplyDelete
  3. I think I will do a common-lisp.net project (with git repository) *and* a CDR.

    I know I am in dinosaur land :)

    Marco

    ReplyDelete