Generics Everywhere CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM 1 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
About me Experienced with backend, frontend, mobile, desktop, ML, databases. Blogger, public speaker. Author of .NET Internals Cookbook. http://blog.adamfurmanek.pl contact@adamfurmanek.pl furmanekadam 2 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Agenda Why do we need reusability. Templates in C++. Erasure in Java. Reification in .NET. 3 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Reusability Use of existing assets in some form within the software product development process. 4 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
5 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Why do we need reusability 6 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
7 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Why do we need reusability 8 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Generic programming Style of computer programming in which algorithms are written in terms of types to- be-specified-later that are then instantiated when needed for specific types provided as parameters. 9 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Generic programming Not new – started in ML in 1973. Most popular on function and class level. Multiple flavours – templates, generics, type classes, polytypic functions. Used for code reuse, static-time reflection, metaprogramming. 10 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Templates in C++ 11 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
12 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
13 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
C++ templates C++ „copies” each template and replaces type placeholder with actual type. Then it tries to compile it. If it works – great! 14 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
15 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
16 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
17 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
How to see the code? 18 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
19 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
20 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
21 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
22 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
23 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
What is T? template<typename T> template<typename T = void> template<typename... Ts> template<int I> template<typename K, typename V, template<typename> typename C = my_array> 24 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
25 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
26 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
27 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
28 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
29 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Raytracing https://github.com/phresnel/metatrace 30 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
C++ templates PROS CONS Relatively easy to understand. „Something” needs to instantiate the type for given parameter. Doesn’t require sophisticated runtime support. Works without any notion of „supertype” or A lot of machine code duplication. „base type” for all types. We cannot instantiate the code in runtime. Doesn’t require extensive constraints for passed types. Compiler messages are often less than ideal. Can be easily specialized for specific types. We cannot constrain input type declaratively – Works nice with const expressions. we can only use metaprogramming tricks. Turing complete – perfect for compile time calculations. 31 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
JVM Erasure 32 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Java type system OBJECTS PRIMITIVE VALUES java.lang.Object is the root of the class Primitive types correspond to primitive values. hierarchy. Every class has Object as a These include int, short, long, byte, char, float, superclass. All objects, including arrays, double, boolean. implement the methods of this class. We can implicitly convert between them (most Provides method for equality checking of the times). (equals), calculating hashcode (hashCode), checking runtime type (getClass), serializing to There is a reference type for each primitive string literal (toString). type respectively (Integer, Short, Long, Byte, Character, Float, Double, Boolean). String is an Object. 33 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
34 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
35 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
36 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
37 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
38 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
JVM erasure Java replaces generic placeholder with java.lang.Object. In other words, it gets rid of the generic type. There is one version of the code – no copying of generic for different types. It then compiles the code with the java.lang.Object as a generic type. 39 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
40 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
41 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
42 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
43 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
44 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
45 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
46 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
47 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
48 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
49 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
50 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
51 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
52 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
53 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
54 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
55 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
56 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
57 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
JVM erasure PROS CONS Maintains backwards comaptibility. We lose the type information. We have a performance hit for primitive types. Stores one code for all types. We don’t have full compiler support and type Works with higher kinded types (partially). safety. We can constrain generic parameter (bounds). It is harder to perform optimizations for Relatively easy to implement. particular types. Works with covariance and contravariance We need to encode „trivial” things with type better. system. We cannot provide specialization for given type. 58 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Reification in .NET 59 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
.NET Type system Mostly based on Java type system. System.Object is the root of the hierarchy. Primitive types are called value types . Users can create their own value types (structures). Few more things: delegates, events, expression trees, dynamic. 60 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
61 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
62 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
63 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
64 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Reification in .NET .NET replaces type placeholder with System.__Canon for reference types and actual type for value types. It has one code for all reference types and different code for each value type. It doesn’t lose the generic type. Generics are reified and remember their type . 65 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
66 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
67 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
68 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
69 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
70 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
71 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
72 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
73 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
74 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
75 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
76 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
77 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
78 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
79 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK
Recommend
More recommend