# How dose Q distinguish trap and monadic functional amend using &quot;@&quot;

**URL:** <https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 15, 2016, 2:37pm UTC](https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351 "2016-11-15T14:37:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhuoql](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@zhuoql](https://forum.kx.com/u/zhuoql)\
**Post date:** [November 15, 2016, 2:37pm UTC](https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351/1 "2016-11-15T14:37:00Z")

</div>

See:

http://code.kx.com/wiki/JB:QforMortals2/functions#Apply\_.28.40.29\_for\_Monadic\_Functions

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

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

Is it determined at compile time or runtime?&nbsp;

Thanks.

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

---

<div class="post-metadata">

**Author:** ![sieber](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@sieber](https://forum.kx.com/u/sieber)\
**Post date:** [November 15, 2016, 2:49pm UTC](https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351/2 "2016-11-15T14:49:00Z")

</div>

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.&nbsp;

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

>

---

<div class="post-metadata">

**Author:** ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)\
**Post date:** [November 15, 2016, 11:23pm UTC](https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351/3 "2016-11-15T23:23:00Z")

</div>

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

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

&nbsp; “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. &nbsp;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”. &nbsp;in C++:

&nbsp; char\*string(int);

&nbsp; 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.

---

<div class="post-metadata">

**Author:** ![zhuoql](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@zhuoql](https://forum.kx.com/u/zhuoql)\
**Post date:** [November 16, 2016, 2:37am UTC](https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351/4 "2016-11-16T02:37:00Z")

</div>

Got it,&nbsp;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&nbsp;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?

&nbsp;

– ZHUO QL (KDr2) [http://kdr2.com](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. &nbsp;the wiki for trap has been updated:

&nbsp; “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. &nbsp;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”. &nbsp;in C++:

&nbsp; char\*string(int);

&nbsp; 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.

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [November 16, 2016, 11:55am UTC](https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351/5 "2016-11-16T11:55:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sieber](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@sieber](https://forum.kx.com/u/sieber)\
**Post date:** [November 16, 2016, 12:00pm UTC](https://forum.kx.com/t/how-dose-q-distinguish-trap-and-monadic-functional-amend-using-quot-quot/11351/6 "2016-11-16T12:00:00Z")

</div>

> 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. &nbsp;
