public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* Splicing lists and vectors into argument lists
@ 2014-05-09 16:19 Per Bothner
  2014-05-10  7:27 ` Helmut Eller
  0 siblings, 1 reply; 5+ messages in thread
From: Per Bothner @ 2014-05-09 16:19 UTC (permalink / raw)
  To: kawa

Please try out this new experimental Kawa feature:
The form '@EXPRESSION' (which is reader syntax
for ($splice$ EXPRESSION)) can be used in a
function application.  The EXPRESSION must evaluate
to a list or vector (more generally a java.util.List)
*or* a native Java array.  The effect is that each
element becomes a separate argument.

For example if lst is a list, then
   (fun x y @lst)
is the same as:
   (apply fun x y lst)

To sum the elements of list/vector/array lst, do:
   (+ @lst)

To concate vectors vec1 and vec2 separated by a single value x, do:
   [@vec1 x @vec2]

Splice notation isn't optimized much so far. If an application contains
a splice argument then we just create an array of the elements and
invoke the applyN method.  This means there is no function-specific
optimization.  Which also means if inlined compilation behaves
differently from run-time application you get the latter behavior.
(This can happen when resolving overloaded methods, for example.)

The plan is to do some optimization, starting with array/list
constructors.  Calling varargs methods can also be optimized: If all
the splice arguments correspond to a varargs array, then the compiler
should generate code to create the varargs array and then emit a regular
'invoke' instruction.

Right now Kawa follows Java when handling varargs, in that
you can optionally in the final argument pass an explicit array
which is treated as a varargs array.  This will be deprecated
soon, and ultimately disallowed.  E.g. the following is currently
allowed:
   (define args (object[] 4 5 6))
   (java.lang.String:format "<x: %s y: %s z: %s>" args) ; DEPRECATED
       ;; as equivalent to:
   (java.lang.String:format "<x: %s y: %s z: %s>" 4 5 6)
In the future this will have to be written:
   (java.lang.String:format "<x: %s y: %s z: %s>" @args)

Note that the syntax (@TYPE ARG ...) is also used for annotations.
That usage don't conflict for a number of reasons:
- The @TYPE syntax for an annotation is only allowed in function
call position, while a slice is *not* allowed in function call position.
- The "expression" following @ for an annotation must be a class or type,
while for a slice it must be a list/vector/array.
- Annotations are only supported in restricted contexts.

The old syntax (TYPE:@ VALUE) for coercions still works,
but deprecated.  Use (->TYPE VALUE) instead.

However, there is *some* compatibility breakage, since '@' no longer starts
a symbol.  Thus for example I had to quote |Q| in syntaxutils.scm.  This
macro could presumably be replaced by colon notation.

Finally: Don't use keyword values in the elements of splice.
This will soon stop working, but that is a topic for another email.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-05-12 21:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-09 16:19 Splicing lists and vectors into argument lists Per Bothner
2014-05-10  7:27 ` Helmut Eller
2014-05-10 16:51   ` Per Bothner
2014-05-10 18:50     ` Helmut Eller
2014-05-12 21:56       ` Per Bothner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).