Arc Forumnew | comments | leaders | submitlogin
1 point by CatDancer 5632 days ago | link | parent

is there a better solution?

try redefining + in Arc, for example

  (redef + args
    (apply (if (all [isa _ 'table] args)
                table+-better
                orig)
           args))
where redef is

  (mac redef (name args . body)
    `(let orig ,name
       (= ,name (fn ,args ,@body))))


1 point by Adlai 5632 days ago | link

I guess putting the definition of + on hash tables into arc.arc makes sense. Either one of the algorithms is a more high-level procedure than the other behaviors of '+.

I like this 'redef macro. How come it's not in arc.arc already? I can't think of any reason to not have this be there, along with the definitions of other key macros like 'do, 'def, and 'mac.

Now, granted, this function actually contributes nothing to <insert large project here, including news.yc>. However, I think that this function strikes at the heart of the "hackable language" mentality, by making it much easier to quickly modify behavior on the fly. It also sends a message to somebody reading the source code: "One of the key principles in this language is that you can redefine behavior across the board with one macro call."

I think that for quick exploratory patches, it's a better solution than editing the original definition. If I want to modify this behavior, I just have one self-explanatory (redef + ...) form to edit, rather than having to sift through the original '+ definition. It is a clean solution, which adds hackability to the language.

-----

2 points by shader 5632 days ago | link

It's already included in Anarki, which until arc3 came out was the version used by the majority of the arc community. Probably still is.

-----

1 point by CatDancer 5632 days ago | link

I like this 'redef macro

You might also like my extend macro: http://catdancer.github.com/extend.html

You know, I had completely forgotten that I had used + on tables in my example of how to use extend...!

-----

1 point by Adlai 5632 days ago | link

'extend is great! However, I think it's more of a great library/patch, than something that should sit with the core functions. 'redef, on the other hand, is elegant, transparent ('extend is transparentially challenged...), and works well for quick hacks. 'extend seems to me to be more suited for setting in stone some behavior sketched out with 'redef (for example, the method-like behavior that you described in the original thread about 'extend http://arclanguage.org/item?id=8895)

The example you have of + on tables uses the algorithm that conses up a storm...

-----

1 point by CatDancer 5632 days ago | link

One thing I like about 'extend is that I can run it several times while developing an extension without my older, buggy versions remaining on the call chain. Try actually hacking with this version of 'redef, it turns out to be really quite a pain!

  arc> (redef + args
         (apply (if (sll [isa _ 'table] args)
                     table+-better
                     orig)
           args))
oops, typo, try again

  arc> (redef + args
         (apply (if (all [isa _ 'table] args)
                     table+-better
                     orig)
           args))
oops, this 'redef is now calling the previous 'redef, I'm stuck!

On the other hand I don't think the explicit separation in 'extend between the test of whether to apply the extension from the implementation of the extension has turned out to be all that useful, so maybe some combination of the two would be better.

-----

1 point by Adlai 5631 days ago | link

Rudimentary hack to fix that:

  (= old-def (table))

  (mac redef (name args . body)
    `(let orig ,name
       (= (old-def ',name) orig)
       (= ,name (fn ,args ,@body))))

  (mac undef (name)
    `(= ,name (old-def ',name)))
I tried storing a list as each value in the table, and popping or pushing to/from the list in redef and undef, so that you could step backwards more than just one definition at a time. However, that made the entire thing much more unstable, because it turns out that 'push and 'pop rely on a bunch of functionality which breaks quite easily if you're messing with the internals.

So, this stripped down version is my little 'redef/'undef hack-pack.

-----