Disclaimer

The content of this blog is my personal opinion only. Although I am an employee - currently of Nvidia, in the past of other companies such as Iagination Technologies, MIPS, Intellectual Ventures, Intel, AMD, Motorola, and Gould - I reveal this only so that the reader may account for any possible bias I may have towards my employer's products. The statements I make here in no way represent my employer's position, nor am I authorized to speak on behalf of my employer. In fact, this posting may not even represent my personal opinion, since occasionally I play devil's advocate.

See http://docs.google.com/View?id=dcxddbtr_23cg5thdfj for photo credits.

Thursday, February 09, 2012

Mixing array and name syntax

So, straight array syntax and straight name=value is handled in many ways
Array: [ a, b, c, d ]
Struct: { k1=v1, k2=v2 }
Or , paren-less (or implicit parens):
Array:  a, b, c, d
Struct:  k1=v1, k2=v2

Q: but what about intermixing - having a key-value appear in the middle of a list of values:
a, b, c, d=e, f, g, h
Should this be interpreted as a struct in an array, or what?

(Again, emphasizing: this is just for convenience. Explicit parens always available.)


(1) implicit struct element

[ a, b, c, {d=e}, f, g, h]

(2)  explicit in same array/struct, inside/in addition to sequence

A different way of looking at it might be to say "All array notation is really just implict name=value pairs":

I.e.
Array: [ a, b, c, d ]
is equivalent to
Array: { 0:a, 1:b, 2:c, 3:d }
This would suggest that
[ a, b, c, d=e, f, g, h]
should be
{ 1:a, 2:b, 3:c, d:e, 4:e, 5:f, 6:g, 7:h }
although even that is not so explicit as one might like: we may want to emphasize that elements "named" d and 4 actually point to the same value, not to different elements that happen to have equal values.

(3) explicit in same array, but outside of sequence

Rather than creating an aklis to the positional notation, put iot oustide positional:

[ a, b, c, d=e, f, g, h]
should be
{ 1:a, 2:b, 3:c, d:e, 4:f, 5:g, 6:h }
This seems to be what I have wanted to do most often with keyword arguments for functions:

add( 1, 2) 
add(1,2,modulo=16) 
add(modulo=16,1,2)
we want to refer to the positional "1" and "2" using the same names, irrespective of where the keyword parameter is written.




BOTTOM LINE:  I think I prefer (3) explicit in same array, but outside of sequence

 
 

No comments: