Skip to content

Document ternary operator #6247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thedub2001 opened this issue May 1, 2017 · 11 comments
Closed

Document ternary operator #6247

thedub2001 opened this issue May 1, 2017 · 11 comments
Labels
Component: Documentation Related to Arduino's documentation content

Comments

@thedub2001
Copy link

thedub2001 commented May 1, 2017

Hello I just discovered the the Serial.print() function can be used with conditional testing :

Serial.print(test ? "test=true" : "test=false");

If the "test" variable is true then the output will be :

test=true

If the "test" variable is false then the output will be :

test=false

I just thought this function could appear somewhere in the Serial.print() reference documentation

@cmaglie cmaglie added the Component: Documentation Related to Arduino's documentation content label May 1, 2017
@facchinm
Copy link
Member

facchinm commented May 2, 2017

Well, it's not only about Serial.print , the ternary operator is allowed in any function if the "arguments" type is correct. However, it could be surely documented, but I'd prefer waiting until we merge some of these PRs

@NeatNit
Copy link

NeatNit commented May 14, 2017

I think the ? : operator is a common enough source of confusion to warrant a mention in the reference. I think it would fit nicely under "Further Syntax":

image

@juanpablojofre
Copy link

I'm with @NeatNit!!
The ternary operator should be documented as part of the syntax of the language.
Where I disagree with @NeatNit is in the location.
Since it's an operator, it should be listed as such.
I would suggest a new header: Conditional Operator
and the entry underneath as:

  • ? : (ternary operator)

@NeatNit
Copy link

NeatNit commented May 22, 2017

It would be lonely all by itself under that header. 😢

@cousteaulecommandant
Copy link
Contributor

Definitely not under "Further syntax"; this is just a plain old operator. Maybe under "Boolean operators".

@feikname
Copy link
Contributor

Why not under control structures? The ternary operator is esssentially a one-line alternative to if {} else { } blocks.

@cousteaulecommandant
Copy link
Contributor

Why not under control structures?

I'd rather not, since it's quite different from a control structure in how it's used – the statements in that section are typically used with blocks of code, which doesn't work with ? :.
However it might be possible to put the documentation of ? : as a section of the documentation for if as is done with comparison operators (and if not, I'd definitely link to ? : on the "See also" section of if).

@thedub2001
Copy link
Author

I found another strange operator and I can't find documentation, perhaps it could be gathered at the same place in future documentation :

for (auto dataobj : data){
    Serial.println(dataobj.key);
}

example from https://bblanchon.github.io/ArduinoJson/doc/decoding/

@feikname
Copy link
Contributor

feikname commented Jun 4, 2017

@thedub2001 It's a range-based for loop.

@cousteaulecommandant
Copy link
Contributor

cousteaulecommandant commented Jun 17, 2017

for (auto dataobj : data)

I'd classify that as "advanced C++ 'black magic' that is out of the scope of the Arduino documentation".
In fact all the code in that page uses quite a lot of C++ mechanisms such as references, templates, and custom classes with operator overloading, which also exist in the internal Arduino code but are usually opaque to the user.

@per1234 per1234 changed the title Serial.print() reference documentation is missing conditional function Document ternary operator Nov 9, 2017
@per1234
Copy link
Collaborator

per1234 commented Feb 5, 2019

Moved to arduino/reference-en#519

@per1234 per1234 closed this as completed Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Documentation Related to Arduino's documentation content
Projects
None yet
Development

No branches or pull requests

8 participants