CppCon 2016: Ben Deane “Using Types Effectively'

preview_player
Показать описание


C++ has a pretty good type system, and modern C++ gives us a greater ability than ever before to use that type system for good: to make APIs easier to use and harder to misuse, to make our datatypes more closely express our intent, and generally to make code safer, more obvious in function and perhaps even faster.

This is an interactive session - incorporating games played between presenter and audience, even - taking a look at choices available to us as datatype and API designers, and examining how a little knowledge about the algebra of algebraic datatypes can help. We'll see why std::optional and (hopefully soon) std::variant will quickly become an essential part of everyone's toolbox, and also explore how types can be used to express not just the structure of data, but also the behaviour of objects and functions.

Ben Deane
Principal Software Engineer, Blizzard Entertainment


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

Wow, optionals and variants are what I've always wanted and I had no idea. No mare enum/union non-sense and sentinel value non-sense.

wesofx
Автор

Safe brilliant sharing of talk. Thanks Ben Deane and all.

LooJulian
Автор

It was a bit hard to wrap my head around 13:20, but I figured it out smoothly eventually.

An “instance of a function” draws a line from “a value in the world of args” to “a value in the world of results”. To describe a function in terms of types completely, you need an instance of the function for EVERY arg. A complete function has <arg> instances, and each instance line can end in <result> different ways.

So for every “value in the world of args”, you have <result> additional types.

arisweedler
Автор

That is one of the best talks I have seen!

AdasPK
Автор

Great talk, very applicable to other slightly less statically typed environments too.

jjurksztowicz
Автор

Great talk but the Q&A is, as always, really annoying when people don't get a mic...

alexweej
Автор

It's good that this topic is becoming more mainstream, but there are a few errors in his talk. For example, the type `void` in C++ has cardinality 1, not 0. A type with cardinality 0 is called a "bottom type", and is the subtype of all other types. I'm not a C++ guru, but my understanding is that C++ does not have a bottom type, although some of its functionality can be replicated with the `noreturn` attribute from C++11.

NebuPookins
Автор

This is applicable to reduce domain at compile time and also reduce testing process. We should not misunderstand this with run time verification(what i mean is still run time logic has to be tested).

wpavada
Автор

"functional programmers keep saying 'map' and 'filter'..." - but these functions actually are 'map' and 'filter', semantics are completely different from 'transform' and 'remove_if'

AxelStrem
Автор

When this enters the std I'll definitely give it a shot.

Manquia
Автор

You said you wouldn’t use the M-word, but then you did a couple times starting at 47:41 (which is in the Q&A, so it’s okay).

Otherwise great talk!

danielrhouck
Автор

What if the ``?:`` is improved that ``a ? b`` makes an ``optional<decltype(b)>`` and ``a : b`` returns the contents of a when not empty else b?

cmdlp
Автор

Can someone explain to me why char f(bool) is 256^2 instead of 2*256? Am I misunderstanding that the number is supposed to represent the number of possible combinations? I can make a truth table that shows all possible combinations for char f(bool) and it comes out to 512 possibilities.

Danielle_
Автор

20:30 looks like a performance nightmare.

italoaugustooliveira
Автор

11:38 maybe you have to define the function with a 'noexcept'? Otherwise technically there are more possibilities.

andik
Автор

so i would use std::holds_alternative to test for a particular "enum" inside a variant? How much slower is that than just comparing an enum?

perfectionbox
Автор

My experience of writing code for compiled languages is that it's like working with a straitjacket on. My errors were rarely in the code itself, it was syntax errors in the arcane, inscrutable language used to express the types. The code he put up to express phantom types is complete gibberish to me. I don't think I've ever had a bug where I've said "I wish I had a type system to save me from making this same error in the future." I never mix up fixed point and floating point numbers, for instance. When I add a character to a number, it's deliberate, and getting the type system to understand my intention is just getting in the way of what I want to get done. I have, however, wished for more expressive loop constructors so I don't have to ever say again:
for (i=0; i<n; i++) {stuff indexing off of i}
I've also wished time and time again to never have to write another type annotation again.
Thank God that perl came along.

dlwatib
Автор

Disagree with "bool f(bool)" having only 4 values in C++.
I say infinity, e.g.
bool f(bool b) { return random()&b; }
or
bool f(bool b) { return date()&b; }
A function can have an infinite number of definitions in C++.

dnaphysics
Автор

25:30 While it's always nicer to have a compile-time check, this kind of thing (like sanitized vs unsanitized) is the point behind Hungarian notation. While it can't make the compiler check for you, correct Hungarian notation here would be to have some prefix on all unsafe string variable names (as well as functions returning unsanitized strings) and some other prefix on all sanitized string variables (and functions). This makes it as easy as it can be to spot that you're using an unsanitized string somewhere you shouldn't (and you can possibly even write tools to help you). Unfortunately, Hungarian notation has been gravely misunderstood, which is why so many hate it.

MasterHigure
Автор

So many templates. Wonder how using variants and phantom types affects compile time?

babgab
join shbcf.ru