Log In  

@zep:

Any chance we could get the added value back from add(t,v)? I ask because it would simplify code a lot of the time. For example, I have a job manager I'm playing with that has a function like this:

function job_call( cor )
  local job = { cor=cor, is_call=true }
  add( _jobs, job )
  return job
end

If add(t,v) returned v, I could write it like this:

function job_call( cor )
  return add( _jobs, { cor=cor, is_call=true } )
end

Nicer, right? Cleaner, fewer tokens. I think that even lets the compiler implement it as a tailcall.

I know it's just a minor thing, but quality-of-life stuff like this can be really nice to have. (Kinda like built-in is_empty(t), cei(f), or sincos(a) would be, among others, wink, wink, nudge nudge.)

What do you say? I can't think of any way it would break backwards compatibility. Please? :)

P#42541 2017-07-16 11:07 ( Edited 2017-07-16 21:21)

PS: Forgot to mention: I tried replacing the built-in add() with one that does what I'm suggesting, but it just keeps calling the original. Must be because it's a built-in operation.

P#42542 2017-07-16 11:08 ( Edited 2017-07-16 15:08)

This is wish-listed for a future update. I'm looking at breaking api future compatibility in 0.2.0, so it would likely be then (along with a bunch of other api tweaks).

To wrap a system function, you can store a reference to it and then call it inside the re-written version:

_add=add
function add(c,i)
 _add(c,i)
 return i
end

a={1,2,3}
b=add(a,"blah")
foreach(a,print)
print(b)
P#42549 2017-07-16 14:27 ( Edited 2017-07-16 18:27)

That's great about the wishlist, thanks. :)

Turns out it doesn't work at the command line to replace add() with your own code. It keeps calling the original. Try setting add=nil on the command line... it will still work. But it works fine in source code. That's why my attempt to replace it failed while yours works, which is good.

Note to self: test concepts in source.

P#42550 2017-07-16 17:21 ( Edited 2017-07-16 21:23)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 08:36:09 | 0.006s | Q:11