generics everywhere
play

Generics Everywhere CONTACT@ADAMFURMANEK.PL - PowerPoint PPT Presentation

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.


  1. Generics Everywhere CONTACT@ADAMFURMANEK.PL HTTP://BLOG.ADAMFURMANEK.PL FURMANEKADAM 1 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  2. 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

  3. Agenda Why do we need reusability. Templates in C++. Erasure in Java. Reification in .NET. 3 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  4. Reusability Use of existing assets in some form within the software product development process. 4 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  5. 5 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  6. Why do we need reusability 6 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  7. 7 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  8. Why do we need reusability 8 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  9. 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

  10. 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

  11. Templates in C++ 11 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  12. 12 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  13. 13 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  14. 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. 15 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  16. 16 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  17. 17 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  18. How to see the code? 18 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  19. 19 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  20. 20 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  21. 21 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  22. 22 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  23. 23 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  24. 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 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  26. 26 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  27. 27 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  28. 28 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  29. 29 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  30. Raytracing https://github.com/phresnel/metatrace 30 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  31. 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

  32. JVM Erasure 32 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  33. 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. 34 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  35. 35 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  36. 36 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  37. 37 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  38. 38 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  39. 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. 40 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  41. 41 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  42. 42 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  43. 43 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  44. 44 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  45. 45 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  46. 46 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  47. 47 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  48. 48 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  49. 49 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  50. 50 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  51. 51 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  52. 52 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  53. 53 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  54. 54 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  55. 55 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  56. 56 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  57. 57 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  58. 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

  59. Reification in .NET 59 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  60. .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. 61 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  62. 62 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  63. 63 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  64. 64 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  65. 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. 66 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  67. 67 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  68. 68 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  69. 69 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  70. 70 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  71. 71 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  72. 72 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  73. 73 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  74. 74 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  75. 75 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  76. 76 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  77. 77 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  78. 78 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

  79. 79 25.10.2020 GENERICS EVERYWHERE - ADAM FURMANEK

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend