Skip to content
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

esphome::optional fixes #6681

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open

Conversation

twasilczyk
Copy link
Contributor

What does this implement/fix?

esphome::optional has two problems that prevent its use in code generation

  • esphome::nullopt isn't available from cpp_types list (so code generator can't refer to it like to nullptr)
  • you can't initialize optional like you would T (so code generator can't just pass e.g. "some string" to esphome::optional<std::string>) - that's the (8) constructor from this list

By the way, I have a question: why can't we just use std::optional?

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@twasilczyk twasilczyk requested a review from a team as a code owner May 6, 2024 03:15
@codecov-commenter
Copy link

codecov-commenter commented May 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.15%. Comparing base (4d8b5ed) to head (ef7cfac).
Report is 514 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6681      +/-   ##
==========================================
+ Coverage   53.70%   54.15%   +0.44%     
==========================================
  Files          50       50              
  Lines        9408     9576     +168     
  Branches     1654     1687      +33     
==========================================
+ Hits         5053     5186     +133     
- Misses       4056     4067      +11     
- Partials      299      323      +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This allows using std::nullopt / esphome::nullopt in generated code.
This allows code generator to pass e.g. string literals for functions with
optional<std::string> arguments.
@kbx81
Copy link
Member

kbx81 commented May 6, 2024

By the way, I have a question: why can't we just use std::optional?

There's been some discussion about this on Discord a few times. I don't recall the details, but I don't think there was any specific opposition to using std::optional. If you want to look into migrating to it (perhaps that would be a better use of this PR), I don't think we'd be opposed. That said, some of the toolchains may be a bit dated, so if you chase it, be sure to test very, very thoroughly.

@twasilczyk
Copy link
Contributor Author

Ouch. I didn't mean to make a huge change out of this (I did migrations like that at work, they can be nasty). And I already aim at other large change to work on (esphome/feature-requests#1568)...

Let me try to fix this one for now (I see some tests are not passing) and maybe I'll pick up migration in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants