Arc Forumnew | comments | leaders | submitlogin
How about a syntax to convert function arguments?
6 points by CatDancer 5607 days ago | 5 comments
I write a lot of functions like

  (def f (x)
    ...)
where sometimes I'll be calling f with an object and sometimes with the id of an object; in the function I may need the obj

  (def f (x)
    (let ob (obof x)
      ...))
or I may need the id

  (def f (x)
    (let id (idof x)
      ...))
where obof passes through an object unchanged and converts an id to an object, and idof does the opposite.

What if we had a syntax to convert function arguments:

  (def f ((c ob obof))
    ...)
Another example would be the testify functions; instead of

  (def mem (test seq)
    (let f (testify test)
      (reclist [if (f:car _) _] seq)))
mem could be

  (def mem ((c test testify) seq)
    (reclist [if (test:car _) _] seq))


3 points by rntz 5606 days ago | link

The only thing that bothers me about this is the same thing that bothers me about 'o for optional arguments: that it messes with destructuring.

    arc> (let (o x) '(1 2) x)
    2     ;expected result
    (1 2) ;actual result
The reason for this is that `(let ,var ,exp ,@body) translates into `((fn (,var) ,@body) ,exp)), and so if var is a list beginning with 'o, it gets interpreted as an optional argument. Likewise:

    arc> (let (c d e) '(1 2 3) e)
    3 ; current result
    Error: "reference to undefined identifier: _e" ; result with argument conversion

-----

2 points by CatDancer 5606 days ago | link

I agree, I'd like to have some syntax for hacking function arguments that doesn't interfere with destructuring.

-----

2 points by rntz 5606 days ago | link

There are at least two options here which don't interfere with existing syntax; I'll demonstrate using optional arguments rather than argument conversion for simplicity:

    (def foo (x y ('o z)) ...)
This doesn't interfere because binding 'quote is more or less illegitimate in arc. (The reason for this lies in the underlying scheme - if 'quote is lexically bound as an identifier, it doesn't function as a special form.)

    (def foo (x y (:o z)) ...)
This doesn't interfere because binding to ':o in arc is more-or-less useless, because ssyntax expands away ':o to 'o. The same is true of '+o.

-----

2 points by shader 5606 days ago | link

I personally like the :o, maybe just because it reminds me of CL. Anyway, it shouldn't be too hard to add a special case for ': in ssyntax if it is the first character.

After all, wouldn't that be an error anyway? Therefore redefining it would not interfere with normal usage of ':. In fact, it makes sense to have different versions for each ssyntax if it is at the beginning, end or middle.

-----

2 points by CatDancer 5606 days ago | link

I wonder about reversing the converter function and the variable:

  (def mem ((c testify test) seq)
    (reclist [if (test:car _) _] seq))
Now it looks like "call testify on test".

-----