How dose Q distinguish trap and monadic functional amend using "@"

See:

http://code.kx.com/wiki/JB:QforMortals2/functions#Apply_.28.40.29_for_Monadic_Functions

http://code.kx.com/wiki/Reference/trap

 When I write @[n1; n2; n3], how does Q know it is an Amend or a Trap? 

Is it determined at compile time or runtime? 

Thanks.

– ZHUO QL (KDr2) http://kdr2.com

q/k is not compiled but interpreted, so the check is at runtime.

for the amend the first parameter has to be a map with explicit domain. 

So i guess if the first parameter is such a map, amend is used otherwise trap.

>for the amend the first parameter has to be a map

i believe markus is correct.  the wiki for trap has been updated:

  “Triadic @ and . are trap /when x is a function/.”

@ is overloaded like many other operators eg: dyadic/triadic ‘?’

q)?[1 2 0;2]

1

q)?[3;2]

1 0 1

both of the ? expressions are dyadic, but the first is ‘find’ and the second is ‘rand’.

note that ? also has two triadic forms (vector conditional and select)

to understand how q achieves this, recall that you can get the type of any value.  when @, . or ? is called with three arguments, the q function checks the type of the arguments* and then decides what function (trap/amend,find/rand) applies.

it’s “overloading”.  in C++:

  char*string(int);

  char*string(struct timeval);

string behaves according to it’s argument.

*it seems that q only needs to check the type of the first parameter to determine what function @ is.

..this explains the order of arguments to dyadic ‘?’ - it’s unusual to have control in y and data in x.

Got it, Thank you all.

I had thought about the overloading, but at that moment, I thought a map is (or acts as) a (partial) function too, so this design is not very self-consistent.

So how do we usually do the overloading in Q? Is it doing some predicating on the argument(using type/count/… etc) then going to different branches the right way?

 

– ZHUO QL (KDr2) http://kdr2.com

On Wednesday, November 16, 2016 7:36 AM, Jack Andrews <effbiae@gmail.com> wrote:

>for the amend the first parameter has to be a map

i believe markus is correct.  the wiki for trap has been updated:

  “Triadic @ and . are trap /when x is a function/.”

@ is overloaded like many other operators eg: dyadic/triadic ‘?’

q)?[1 2 0;2]

1

q)?[3;2]

1 0 1

both of the ? expressions are dyadic, but the first is ‘find’ and the second is ‘rand’.

note that ? also has two triadic forms (vector conditional and select)

to understand how q achieves this, recall that you can get the type of any value.  when @, . or ? is called with three arguments, the q function checks the type of the arguments* and then decides what function (trap/amend,find/rand) applies.

it’s “overloading”.  in C++:

  char*string(int);

  char*string(struct timeval);

string behaves according to it’s argument.

*it seems that q only needs to check the type of the first parameter to determine what function @ is.

..this explains the order of arguments to dyadic ‘?’ - it’s unusual to have control in y and data in x.

In q, I think, only built-in functions support overloading. UDFs do not support overload.

If your UDF’s various overloads have the same number of arguments, however, you may choose to perform different operations within your UDF depending on the types of the individual arguments.

So how do we usually do the overloading in Q? Is it doing some predicating on the argument(using type/count/… etc) then going to different branches the right way?

i think it’s better to just write different functions and use them accordingly.