Back to questions
JuniorTheoryCommon
What does the program print?
struct Day: CustomStringConvertible {
var n: Int
var description: String { "day \(n)" }
}
var today = Day(n: 3)
today.n += 1
print(today)day 4
- Expecting
Day(n: 4): thinkingprintignoresdescription. Once the type declaresCustomStringConvertible,printshows the textdescriptionreturns. - Expecting
day 3: treatingdescriptionas text saved at creation. It is computed on every read, so it showsnaftern += 1.
- What would the program print without
: CustomStringConvertiblein the declaration ofDay? - What does string interpolation print when
todayis placed inside a string?