Arc Forumnew | comments | leaders | submitlogin
1 point by akkartik 4382 days ago | link | parent

Awesome!

a) if-box is a kinda contrived example. Perhaps we can find something better?

b) What's the difference between w/uniq and w/box? Both seem to create boxes. Could I just say this at http://github.com/Pauan/nulan/blob/5dfddacfbb/nulan.html#L44...?

  $mac if-box ->
    w/uniq temp
      'w/box temp = 5
         if temp
           temp + 1
c) I tried running this and got the following:

  TypeError: Cannot call method 'chunkSize' of undefined
I'm gonna play with this more!


2 points by Pauan 4382 days ago | link

"if-box is a kinda contrived example. Perhaps we can find something better?"

If you have a better suggestion, I'm all ears. But I settled on that because A) it's simple, B) it's short, C) it's something that does actually come up in practice, so it's not too contrived.

---

"What's the difference between w/uniq and w/box? Both seem to create boxes."

Well, to explain this... unique variables are actually implemented as anonymous boxes. So implementation-wise, there's not much difference.

The difference is in the actual macros "w/box" and "w/uniq". Here's the definition for them:

  $mac w/box -> @args body
    'w/new-scope
       | var ,@args
       | body

  $mac w/uniq -> @args body
    'w/box ,@(args.map -> x 'x = make-uniq;)
       body
In other words, these are the same:

  w/box foo = 5
    foo

  w/new-scope
    | var foo = 5
    | foo
And these are the same:

  w/uniq foo
    foo

  w/box foo = make-uniq;
    foo

  w/new-scope
    | var foo = make-uniq;
    | foo
In other words, "w/box" puts a value into a box. "w/uniq" puts a box into a box.

They serve exactly the same purpose as "let" and "w/uniq" in Arc.

If you're asking why you can't just do this:

  $mac if-box ->
    w/box temp
      'w/box temp = 5
         if temp
           temp + 1
The answer is that "quote" inserts boxes for global symbols, but values for local symbols. That is, this macro...

  $mac foo ->
    w/box bar = 5
      'bar + 2
...returns "5 + 2", not "#<box bar> + 2". This is an unfortunate inconsistency which happens because I'm using JavaScript variables for the sake of speed.

I thought about having it throw an error for local symbols, which would have required you to write the macro like this:

  $mac foo ->
    w/box bar = 5
      ',bar + 2
But I decided that it wasn't worth it. Now that I think about it, I wonder if it would be possible to hack something up so you no longer need w/uniq...

---

"I tried running this and got the following"

It works fine for me. And in fact, the code snippet you posted seems exactly the same as what is already in the tutorial. Maybe you intended to paste something else?

As for the error... yeah, there are still some cryptic errors. I plan to make them more meaningful later.

-----

1 point by Pauan 4382 days ago | link

"Now that I think about it, I wonder if it would be possible to hack something up so you no longer need w/uniq..."

After thinking about it some more, I realized it won't work very well. It would require me to make all variables global, which would slow things down and be more verbose.

-----

1 point by Pauan 4382 days ago | link

Whoops, those examples should use "box" rather than "var". But otherwise they're correct.

-----