Are there any conventions for printing optional values?
Suppose I want to overload operator<<
for an optional<T>
class t开发者_运维知识库emplate. How would I print the "absent value", and how would I print a "real value" x?
none
some x
or
[]
[x]
Or should I literally print nothing for the first case and x
for the second? How is this normally handled?
I like the option of print None
and Some x
. I think that this immediately describes what's going on (especially for people familiar with Haskell).
Personally, I would not use the []
and [x]
alternative, because many languages use the square brackets to denote some sort of list. If I were to see that output, I would immediately be thinking that a list had been printed, as opposed to an optional type.
In the absence of any context, I would think of an optional as a special case of a collection, that either is empty or has one member.
You probably already have a convention for how to print collections or compound objects, but something like {}
if it's empty or {x}
if it has the value x
would seem reasonable. If you print out an empty vector as none
and a vector with three elements as some x y z
, then by all means apply the same convention to an optional type :-)
I'm not aware of any specific convention. Personally I'd print (null)
when the value is missing and the actual value otherwise.
It depends on the type. If you want a string to be printed, it could be "" or "Fred". If it is an array, it could be {} or { 1, 2, 3 }.
Following this video or the code attached in the web page can help you. Pretty Printer
The choice of behavior should probably be based on why you're implementing an output operator. If it is mostly for debugging purposes it is important to provide a visual clue that a value is missing. Printing an existing value between square brackets or just the open and closed brackets if the value is missing is a valid approach, given that square brackets are often used to indicate optionality, e.g. in command help messages.
On the other hand if this is meant to be a general purpose output operator the best approach is probably to print existing values as you would for the non-optional underlying type and the empty string for missing values.
The UNIX and the Echo.
There dwelt in the land of New Jersey the UNIX, a fair maid whom savants traveled far to admire. Dazzled by her purity, all sought to expose her, one for her virginal grace, another polished civility, yet another for her agility in performing exacting tasks seldom accomplished even in much richer lands. So large of heart and accomodating of nature was she that the UNIX adopted all but the unsufferably rich of her suitors. Soon many offspring grew and prospered and spread to the ends of the earth.
Nature herself smiled and answered to the UNIX more eagerly than to other mortal beings. Humbler folk, who knew little of more courtly manners, delighted in her echo, so precise and crystal clear they scarce believed she could be answered by the same rocks and woods that so garbled their own shouts into the wilderness. And the compliant UNiX obliged with perfect echoes of what ever she was asked. When one impatient swain asked the UNIX, 'Echo nothing', the UNIX obligingly opened her mouth, echoed nothing, and closed it again.
'Whatever do you mean,' the youth demanded, 'opening your mouth like that? Henceforth never open your mouth when you are supposed to echo nothing!' And the UNIX obliged.
'But I want a perfect performance, even when you echo nothing,' pleaded a sensitive youth, 'and no echoes can come from a closed mouth.' Not wishing to offend either one, the UNIX agreed to say different nothings for the impatient youth and the sensitive youth. She called the sensitive nothing '\n.'
Yet now when she said '\n,'she was really not saying nothing so she had to open her mouth twice, once to say '\n,' and once to say nothing, and so she did not please the sensitive youth, who said forthwith, 'The \n sounds like a perfect nothing to me, but the second ruins it. I want you to take back one of them.' So the UNiX, who could not abide offending, agreed to undo some echoes and called that '\c'. Now the sensitive youth could hear a perfect echo of nothing by asking for '\n' and '\c' together.
But they say that he died of a surfeit of notation before he ever heard one.
-- Doug McIlroy
精彩评论