Are there any conventions for printing optional values?

Suppose I want to overload operator<< for an optional<T> class template optional<T> . How to print the "missing value" and how to print the "real value" x?

 none some x 

or

 [] [x] 

Or should I literally not print anything for the first case and x for the second? How is this usually handled?

+4
source share
6 answers

I like the print option of None and Some x . I think this immediately describes what is happening (especially for people familiar with Haskell).

Personally, I would not use the alternative [] and [x] , because many languages ​​use square brackets to indicate a kind of list. If I could see this output, I would immediately think that the list was printed, unlike the optional type.

+1
source

In the absence of any context, I would think of an optional as a special case of a collection that is either empty or has one member.

You probably already have an agreement on how to print collections or composite objects, but something like {} if it is empty or {x} if it has an x value seems reasonable. If you print an empty vector as none and a vector with three elements like some xyz , then be sure to apply the same convention to the optional type :-)

+1
source

I do not know any specific agreement. Personally, I print (null) when the value is absent, and the actual value otherwise.

0
source

It depends on the type. If you want to print a string, it can be “or“ Fred. ”If it is an array, it can be {} or {1, 2, 3}.

After that, the video or code attached to the web page can help you. Pretty printer

0
source

The choice of behavior should probably be based on why you are executing the output statement. If this is mainly for debugging purposes, it is important to provide a visual hint that the value is missing. Printing an existing value between square brackets or only open and closed brackets if there is no value is a valid approach, given that square brackets are often used to indicate optionality, for example. in command messages.

On the other hand, if this means that the general-purpose output operator is best suited for printing existing values, as well as for an optional base type, and an empty string is for missing values.

0
source
 The UNIX and the Echo. 

They lived in New Jersey, UNIX, a beautiful maiden whom scientists sent far away to admire. Blinded by her purity, everyone sought to expose her, one for her virgin grace, another polished politeness, another because of her dexterity in completing difficult tasks that were rarely performed even on much richer lands. Such a huge heart and an abundance of nature was that UNIX accepted everything except the incorrigibly rich of her suitors. Soon, many children grew and flourished and spread to the ends of the earth.

Nature herself smiled and responded more readily to UNIX than to other mortal beings. The vague people, who knew little about court manners, delighted with their echoes, are so accurate and crystal clear that they hardly believe that they can be answered by the same stones and forests that distorted their cries in the desert. And the corresponding UNiX owes an excellent echo of what was ever asked. When an impatient Swine asked UNIX, “Echo nothing,” UNIX kindly opened his mouth, repeated nothing, and closed it again.

“Whatever you say,” the young man demanded, “so opening your mouth? From now on, never open your mouth when you must not repeat anything! And UNIX is obliged.”

“But I want a great job, even when you don't say anything,” the sensitive young man pleaded, “and no echo can come from a closed mouth.” Not wanting to offend any, UNIX agreed to say different words for impatient youth and sensitive youth. She called nothing insensitive '\ n.'

But now, when she said "\ n", she really didn’t say anything, so she had to open her mouth twice, say "\ n" once and say nothing once, and therefore she didn’t like the sensitive youth who immediately said: “It sounds completely unimportant to me, but the second one is falling apart. I want you to take one of them. Therefore, UNiX, which could not be offended, agreed to cancel some echoes and called it“ \ c. ”Now sensitive youth could hear the perfect echo nothing by asking "\ n" and "\ c" together.

But they say that he died from an overflow of notes before he ever heard him.

- Doug McIlroy

0
source

All Articles