Technology

CppCon 2019: Time Travel: Gradual Typing in C++

Hyrum Wright

C++ is a dynamically typed language

example with sso_string and union

If somebody can (ab)use your API, they will

Types Encode constraints

int timeout;
int number_of_pumpkins = 6*timeout;

better:

void set_deadline (absl::Duration deadline);


See “Time Programming Funamentals” by Greg Miller

Abseil Time API

  • Valid invalid operations: Duration + Duration gives Duration;
Duration + Time gives Time;
Time + Time is invalid, etc

Gradual Typing

  • python: adds annotation

Goals

  • Automated (almost) everything
  • Keep code buildable (each multiple steps)
  • Keep code readable
  • Eventually converge at a fixed point

Things that don’t work

  • Monolithic changes
  • Variable type
    • Most integers don’t represent time
  • Variable name
    • What does timeout_seconds actually mean?
  • Comments super unreliable

Types of transformations

  • Expression
    • Simplification
    • Comparison
    • Addition and Subtraction
    • Distribution
  • Variable
    • Local, global, class members
  • Inter-function
    • Parame and value

Expression: Simplification

  • Makes matching easier
  • clang-tidy code for matcher
  • code for fixer

Expresion: Comparison

Inter-function return values

Seeding Type Information

  • Identify high value targets

Reactions

  • You added a bug … oh
  • Thanks
  • (crickets)

Other applications

  • Converting to std::string_view
  • Ownership deduction (i.e. std::unique_ptr)

GitHub.com/llvm-mirror/clang-tools-extra/tree/master/clang-tidy/abseil

Questions

  • How to justify? “We should be able to change our software”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.