Making Types Printable

I’m working on making the Vector struct printable. I found that the Printable Protocol and the println() functions have been replaced by the CustomStringConvertible Protocol and print() function respectively. The description: String property remains as described.

When I extend the CustomStringConvertible Protocol in the Vector struct, all is well however the print() function doesn’t work in the playground.

Try the following in your playground and see what you get.

struct Foo : CustomStringConvertible {
    
    let details : String!
    
    init () {
        details = ""
    }
    
    init (_ v:String) {
        details = v
    }
    
    var description: String {
        return "Foo (\(details!))"
    }
}

print (Foo ())
print (Foo ("1"))