cancel
Showing results for 
Search instead for 
Did you mean: 

Is this similar to function alias?

mamsds
New Contributor II

I am studying the official example code from here: https://github.com/kxcontrib/websocket/blob/master/AppendixB/pubsub.q . After playing around for a while, I can get the publisher/subscriber model work but still I don't understand what exactly this line (https://github.com/kxcontrib/websocket/blob/ad2f0b268afaee1fc5f4dda2fc2467440c7e2f0c/AppendixB/pubsu...) is doing.

After checking the Reference card (https://code.kx.com/q/ref/), I know that insert is a built-in function and Overloaded glyphs (https://code.kx.com/q/ref/overloads/) says that : (colon) only has two possible meanings (https://code.kx.com/q/ref/overloads/#colon).

 

So here comes the question, what does

upd:insert;

do? Does it mean that we declare upd as an alias of insert, so calling upd will be exactly the same as calling insert? (But I did try it myself, eliminating this line and directly call insert here: https://github.com/kxcontrib/websocket/blob/ad2f0b268afaee1fc5f4dda2fc2467440c7e2f0c/AppendixB/fh.q#... no, it doesn't work lol)

1 ACCEPTED SOLUTION

rocuinneagain
Contributor
Contributor

Yes defining upd in this way means it behaves the same as insert (mostly)

 

q)upd:insert
q)tab:([] a:1 2)
q)insert[`tab;enlist 3]
,2
q)tab
a
-
1
2
3
q)upd[`tab;enlist 4]
,3
q)tab
a
-
1
2
3
4

 

But there are differences. 'insert' is a built in operator which cannot be passed as the first item by reference over a handle.

(This is causing the issue you are seeing)

 

q)value(`upd;`tab;enlist 5) //Pass by reference succeeds for user defined function
,4
q)value(`insert;`tab;enlist 6) //Pass by reference fails for operator
'insert
  [0]  value(`insert;`tab;enlist 5)
       ^
q)value("insert";`tab;enlist 6) //Pass as parse string succeeds
,5
q)value(insert;`tab;enlist 6) //Pass by value succeeds 
,6

 

 User defined functions can only use prefix notation whereas operators can be used prefix or infix.

 

q)`tab insert enlist 7 //Infix with operator succeeds
,7
q)`tab upd enlist 8 //Infix with user defined function fails
'type
  [0]  `tab upd enlist 8
       ^
q)insert[`tab;enlist 8] //Prefix with operator succeeds 
,8
q)upd[`tab;enlist 9] //Prefix with user defined function succeeds 
,9

 

 

 

View solution in original post

1 REPLY 1

rocuinneagain
Contributor
Contributor

Yes defining upd in this way means it behaves the same as insert (mostly)

 

q)upd:insert
q)tab:([] a:1 2)
q)insert[`tab;enlist 3]
,2
q)tab
a
-
1
2
3
q)upd[`tab;enlist 4]
,3
q)tab
a
-
1
2
3
4

 

But there are differences. 'insert' is a built in operator which cannot be passed as the first item by reference over a handle.

(This is causing the issue you are seeing)

 

q)value(`upd;`tab;enlist 5) //Pass by reference succeeds for user defined function
,4
q)value(`insert;`tab;enlist 6) //Pass by reference fails for operator
'insert
  [0]  value(`insert;`tab;enlist 5)
       ^
q)value("insert";`tab;enlist 6) //Pass as parse string succeeds
,5
q)value(insert;`tab;enlist 6) //Pass by value succeeds 
,6

 

 User defined functions can only use prefix notation whereas operators can be used prefix or infix.

 

q)`tab insert enlist 7 //Infix with operator succeeds
,7
q)`tab upd enlist 8 //Infix with user defined function fails
'type
  [0]  `tab upd enlist 8
       ^
q)insert[`tab;enlist 8] //Prefix with operator succeeds 
,8
q)upd[`tab;enlist 9] //Prefix with user defined function succeeds 
,9

 

 

 

View solution in original post