Procs, their arity and how many of the args are actually requested

Let’s say we have this proc

Proc(Int32, Int32, Int32, String).new { |x| "foo"} 

The proc can receive 3 Int32 args, and a .arity returns correctly 3.

But the block actually uses only one of them (x in the example). Is there anything I can throw on that proc to receive 1 (or if it would use |x, y| then 2, and so on)?

As the compiler complains if I were to use more than the proc offers, it must know how many are used, question is whether this information is also available to me, either directly or with some macro!?

The general answer is “no”, but I’d like to know why you need this. The Proc takes 3 args. It just uses one of them. Outside of the proc definition that knowledge is lost…

1 Like

To clarify: omitting parameters from a proc or block is just a convenient way to not name them if you don’t want to use them. But it still creates a proc or block that takes that many arguments.

1 Like

It’s for handing the proc over to some external (in my case it’s ruby). The other side wants to know how many arguments will be used. Its possible to write the count myself, but it feels unnecessary (and just another possibility to copy paste too much and send the wrong count).

But I can totally live with that. Just asked because it ould have been bad if it was possible and I wouldn’t use it only because I don’t know about it