[Swift-devel] Confusion about evaluation of array arguments

Mihael Hategan hategan at mcs.anl.gov
Tue Dec 11 23:10:30 CST 2012


app() unfolds the array.

strjoin(string[], delim) expects an array as the first argument and its
contract is that it joins the elements of that array.

strcat(...) expects a list of stuff as varargs, converts each of them to
a string, and concatenates the resulting strings. So if you pass an
array, that will be the first of the varargs.

Mihael



On Tue, 2012-12-11 at 22:49 -0600, Michael Wilde wrote:
> Im confused about when a string array, passed as an argument to a app() or primitive, is evaluated as a string of space-separated values, and when its evaluated as an array.  Is this behavior dependent on the receiving primitive?
> 
> A string array passed to an app() and placed on the command line of the app is expanded into space-separated strings.
> 
> The same array passed to @strjoin() yields the expected string of separated array elements.
> 
> But the same array passed to @strcat is not evaluated but instead becomes a single string with the value "fnames:string[int] - Open".
> 
> Is the difference between these two behaviors determined by the primitive or context in which the array is evaluated, with @strjoin() and app() command-line expansion performing an expansion of the array's value, while being referenced by @strcat() does not force such evaluation?
> 
> This is the behavior in the test example below.
> 
> - Mike
> 
> 
> type file;
> 
> app (file o) sh (string cmd)
> {
>   sh "-c" cmd stdout=@o;
> }
> 
> app shf (file cmd)
> {
>   sh stdin=@cmd;
> }
> 
> app (file o) echo (string a[])
> {
>   echo "array: " a stdout=@o;
> }
> 
> foreach year in [1979:1980] {
>   string find     = @strcat("ls -1 /home/wilde/swift/lab/*.swift");
>   string fnames[] = readData(sh(find));
> //  string cdo      = @strcat("echo ", @strjoin(fnames," "));
>   string cdo      = @strcat("echo ", fnames);
>   file script<single_file_mapper;file=@strcat("script.",year)>;
>   file echo_out<single_file_mapper;file=@strcat("echo.",year,".out")>;
>   echo_out = echo(fnames);
>   script = writeData(cdo);
>   shf(script);
> }
> 
> 
> 
> 





More information about the Swift-devel mailing list