Arc Forumnew | comments | leaders | submitlogin
2 points by thaddeus 5244 days ago | link | parent

'as' is much better - changing my code now :)

I had also created coerce-if for coercing args from an defop arg-req:

  (def coerce-if (v type) 
     "coerce value if not nil"
     (if v (coerce v type)))
only when substituting for "as" I start getting arc-slang-talk:

  (def as-if (type v) 
     (if v (coerce v type)))

   (defop myserv req
      (let id (as-if int (arg-req "id"))
        (pr id))
lol.


1 point by akkartik 5244 days ago | link

More than as-if, I want a really-as, which will convert nil to a default value.

Right now I just have int2, which is like int, but converts nil to 0. Still mulling how to generalize it. Perhaps it doesn't need to be generalized.

Anybody have a better name than int2?

-----

2 points by aw 5244 days ago | link

Maybe a function like 'only', but which defaults to 0 instead of nil?

  (def default0 (f)
    (fn args (if (car args) (apply f args) 0)))

  arc> (default0.int "123")
  123
  arc> (default0.int nil)
  0

-----

1 point by rocketnia 5243 days ago | link

You could even name it "0nly", with a zero. ^_^ That could be a bit deceptive, though.

-----