Make it possible to stringify Swift arguments

Originator:michael.ash
Number:rdar://22328935 Date Originated:2015-08-15
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification: Reproducible:
 
One of the nice features of C's assert function is that it stringifies the argument and prints it as part of the assertion failure. So you can do:

assert(x > 0)

And when it fires, it will tell you not only that an assertion failed, but that it was "x > 0" that failed.

It would be great if Swift's assert, precondition, and similar functions could do the same. And of course it would be especially great if the feature were available as a general language feature, so we could stringify our own function's arguments if needed.

For an implementation, I'd suggest extending the current system where some function default values get treated specially. For example you can get the name of the calling function by specifying `= __FUNCTION__` as the default value of an argument. I'd suggest something like `= #argname` to make the default value be the stringified value of whatever was passed in as argname. A simple assertion function would then look like:

    func assert(@autoclosure condition: () -> Bool, conditionString: String = #condition) {
        if !condition {
            print("Assertion \(conditionString) failed!")
        }
    }

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!