[Swift-user] tutorial/understanding issue 7

Mihael Hategan hategan at mcs.anl.gov
Thu Oct 14 14:35:34 CDT 2010


Inline

On Thu, 2010-10-14 at 12:08 -0500, Daniel S. Katz wrote:

> Continuing my journey through the Swift tutorial
> (http://www.ci.uchicago.edu/swift/guides/tutorial.php), though the
> previous 6 messages are waiting for approval, as I was not a member of
> the swift-users list when I sent them...
> 
> 
> In section 3.5, why can't I do this:
> 
> 
> 
> 
> type messagefile;
> 
> 
> app (messagefile t) greeting (string s[]) {
>     echo s[0] s[1] s[2] stdout=@filename(t);
> }
> 
> 
> messagefile outfile <"q5out.txt">;
> 
> 
> #string words[] = ["how","are","you"];
> string words[];
> words[0] = "how;
> words[1] = "are";
> words[2] = "you";
> 
> 
> outfile = greeting(words);
> 
There is no theoretical reason why you shouldn't be able to do so. It
follows that we are talking about a bug. It was likely not addressed (or
reported) because there is a way to deal with the situation.
> 
> 
> is the issue that swift doesn't know how large to make words[]?
> 
Arrays are dynamic, so not quite. 
> 
> I also tried:
> 
> 
> string words[3];
> 
> 
> but this also didn't work.
> 
A good point. There is, when iterating over an array, a distinction
between an array whose size you know and one whose size is not known
statically. The above type of declaration could be used to provide that
information, and I think it should be added to swift (if missing).
> 
> Do strings need to be assigned when they are declared?  Is this a
> general rule for Swift variables?
> 
No in theory. Variables need to eventually be assigned, otherwise they
are considered "input" variables. But that does not apply to primitives.
So in the case of strings your code should work.

Mihael





More information about the Swift-user mailing list