Talk:Middle three digits

From Rosetta Code

Inspiration

The idea for the task comes from this blog entry: Interesting interview question #1 by Michael Jasper. Thanks. --Paddy3118 12:41, 2 February 2013 (UTC)

Second D entry and return type

Because the middle three digits can have leading zeroes, I would think that the more natural return type would be a string rather than an int. --Paddy3118 19:18, 3 February 2013 (UTC)

I didn't write that code, but I think I can explain it. The return type is a Variant type that can represent any type. In this case it's limited to the types listed: string, char[]. So the function will return a Variant that represents either a string or a char[]. By looking at the returned type (peek) you can tell whether or not an error occured. Fwend 21:54, 3 February 2013 (UTC)
Ah. I get it now. The middle three digits are calculated numerically and then changed to an array of three characters for the non-error return value. Error conditions return a string type. Thanks. --Paddy3118 22:13, 3 February 2013 (UTC)
Of course, you can also just check the length of the returned value, but that would be cowboy programming! :-) Discriminating between a string and a char[] is also not very solid, when safety is concerned. They are both string types. A function could easily return an error message as a char[]. It would be better to throw an error, or if you want to avoid the overhead, to return an Exception object. Then you can be certain.