submit part 4: mythical creature interactions#1
Conversation
ericjenkinson
left a comment
There was a problem hiding this comment.
Good job completing this assignment.
| @@ -28,6 +28,57 @@ struct Creature { | |||
| return Int(numerator / denominator) | |||
| } | |||
There was a problem hiding this comment.
Meets expectations!
It looks like your other branches have parts 1 through 3, and this branch has part 4. I was able to combine them, so there was no problem.
| // } | ||
|
|
||
| // Part 4: Mythical Creature Interactions | ||
| func interactWith(creature: Creature) { |
There was a problem hiding this comment.
This function exhibits a behavior that is not discussed in the content. In Swift, if a function doesn’t explicitly return a value, it returns Void (an empty tuple, ()).
This is why you were seeing () in your output.
Astral Horn is a unicorn and has a magic power strength of 9 points which translates into 34 points of special ability. Both creatures are good and there is no point in them fighting each other. What can we do? ()
This also plays a part in the issue you found when trying to single-print a statement with String Interpolation.
| return Int(numerator / denominator) | ||
| } | ||
|
|
||
| // // Part 4: Mythical Creature Interactions |
There was a problem hiding this comment.
It is considered good style to remove code that is commented out. If you need to leave the code commented out, add a comment explaining why for the reviewer.
|
|
||
| /* The homework is done before this line. However, the func describeCreature has 2 print functions to deliver what I was asked of. If I want to combine the content of the two print functions into one, what is printed inside the console down is done in reverse (i.e. it calls the interactWith function first then the content of the first print function). I don't know why is this and I hope you can help understand this behavior (this is why I separated the content into 2 separate print functions). | ||
| */ | ||
|
|
There was a problem hiding this comment.
Nice catch! The reason why the output from below appears first is due to how String Interpolation works.
\(creature.ability) \(creature.interactWith(creature: creatureArray.randomElement()!))
Swift evaluates every expression inside the string interpolation before constructing the final string for the print statement. The call to creature.interactWifth(creature: ) contains its own print statement or other side effects, those will execute immediately when that expression is evaluated.
In other words, as Swift processes the interpolated expressions, it calls the function, and its side effects (like printing) occur right away, which is why you see its output before the overall string is printed.
You can resolve this issue by modifying interactWith(creature: ) to return a String.
`func interactWith2(creature: Creature) -> String {
switch (self.isGood, creature.isGood) {
case (false, false):
return "Both creatures are evil; it's good that they are fighting each other."
case (false, true):
return "The evil (self.name) is fighting the good (creature.name). Do you think we can convince (self.name) to turn to the right side?"
case (true, false):
return "I guess there is no better time to fight the evil (creature.name), right?!"
default:
return "Both creatures are good and there is no point in them fighting each other. What can we do?" }
}`
This change will preserve the order of output as intended and remove the extra () that are printed.
No description provided.