<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: liron_hazan</title>
    <description>The latest articles on Forem by liron_hazan (@lironn_h).</description>
    <link>https://forem.com/lironn_h</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F317036%2Fbaa7ef33-b03c-4a9e-bfeb-4241a8ec66b7.jpeg</url>
      <title>Forem: liron_hazan</title>
      <link>https://forem.com/lironn_h</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lironn_h"/>
    <language>en</language>
    <item>
      <title>Type Thinking 🤔 P2 - Typeclass</title>
      <dc:creator>liron_hazan</dc:creator>
      <pubDate>Fri, 08 May 2020 15:39:19 +0000</pubDate>
      <link>https://forem.com/lironn_h/type-thinking-p2-typeclass-p76</link>
      <guid>https://forem.com/lironn_h/type-thinking-p2-typeclass-p76</guid>
      <description>&lt;p&gt;In this post I shortly compare:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Haskell &lt;em&gt;Typeclass&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Rust &lt;em&gt;Trait&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Typescript type constraints &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Demystifying some big words (wikipedia definitions):
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Polymorphism&lt;/em&gt; - (many forms) "Polymorphism is the provision of a single interface to entities of different types"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I remember facing "polymorphism" when I just started to learn Java as a first OOP lang (~8 years ago). &lt;br&gt;
The teacher taught us that one of the basics principles of an OOP lang is "&lt;em&gt;polymorphism&lt;/em&gt;". &lt;br&gt;
Whenever I hear of it I think of the famous animal example. having "Animal" class which the "Dog" and "Cat" inherit from and I can iterate a collection of Animals and invoke the "makeSound()" function they all implement cause they all of the same type (object) - "Animal".&lt;/p&gt;

&lt;p&gt;But If being honest I don't remember the last time I used inheritance (even the prototype kind..) I usually define a structure type by an interface and defines another "behavioural" interface containing the methods I need to implement. (a matter of taste? Or a better segregation...).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Ad hoc polymorphism&lt;/em&gt; or just overloading: "Defines a common interface for an arbitrary set of individually specified types". on top of that we have functions overloading - the ability to create multiple functions of the same name with different implementations (handled by the compiler).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Parametric polymorphism&lt;/em&gt; "A way to make a language more expressive, while still maintaining full static type-safety" (Generics..)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Dynamic Dispatch&lt;/em&gt; - The process of selecting which implementation of a polymorphic operation (method or function) to call at run time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Definitions:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;TypeClass&lt;/th&gt;
&lt;th&gt;Trait&lt;/th&gt;
&lt;th&gt;Interface&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="https://en.wikipedia.org/wiki/Type_class"&gt;A type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types.&lt;/a&gt; If a type is a part of a typeclass it should implement the behavior described on that typeclass.&lt;/td&gt;
&lt;td&gt;&lt;a href="https://doc.rust-lang.org/1.8.0/book/traits.html"&gt;A trait is a language feature that tells the Rust compiler about functionality a type must provide&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;We use an &lt;em&gt;interface&lt;/em&gt; to define a shape and we use &lt;em&gt;Generics&lt;/em&gt; constraints for saying that a certain behaviour (or an attribute) is required by a type.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;

&lt;p&gt;Haskell: &lt;br&gt;
Following example is from: &lt;a href="http://learnyouahaskell.com/making-our-own-types-and-typeclasses"&gt;learnyouahaskell&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;data&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;  
                     &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;  
                     &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;  
                     &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kr"&gt;deriving&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Eq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;adRock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Adam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Horovitz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;41&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;mca&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Adam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Yauch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="c1"&gt;-- mca == adRock will be false&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm far of being an haskell developer but it seem to fit the typeclass definition, there's a class Eq, it contains "==" and "/=" functions, &lt;br&gt;
deriving (Eq) tells the compiler to generate instance of the Eq class for the Person type, this way it implements / has the "==" "/=" operators. &lt;/p&gt;

&lt;p&gt;Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;NoisyAnimal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;make_noise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'staic&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'staic&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;NoisyAnimal&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Cat&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;make_noise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'staic&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'staic&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"meow"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;NoisyAnimal&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;make_noise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'staic&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'staic&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;"woof"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;make_noises&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;NoisyAnimal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="c"&gt;//       ----- trait object&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;animals&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="nf"&gt;.make_noise&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;make_noises_with_constraints&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NoisyAnimal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="nf"&gt;.make_noise&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;call_make_noises&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;make_noises_with_constraints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;NoisyAnimal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;{}),&lt;/span&gt;
        &lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;{}),&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nf"&gt;make_noises&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Struct&lt;/em&gt; is a type that is composed of other types. (in Typescript we model in an &lt;em&gt;interface&lt;/em&gt;). In the above example we're implementing the trait obj functionality to 2 structs Cat &amp;amp; Dog. &lt;br&gt;
The make_noises fn iterates a vector of types that implements the trait object (dynamic dispatch) - pretty cool :D.&lt;br&gt;
I also created the make_noises_with_constraints() methos which demonstrates a trait constraint which is more relevant to the concepts I raised..&lt;/p&gt;

&lt;p&gt;Typescript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Instrument&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;play&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Guitar&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Instrument&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;play&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Drums&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Instrument&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;play&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;playPart&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Instrument&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;instruments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;instrument&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;instruments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;instrument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;play&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;playPart&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Guitar&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Drums&lt;/span&gt;&lt;span class="p"&gt;()]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using Generics constraints I'm not limiting the playPart() function for a single type of instruments, we can make sound of many things, e.g. a bottle of beer, so in case that bottle implements the Instrument interface, it will be playable - (which could be a better name actually :P ).&lt;/p&gt;

&lt;p&gt;Cheers,&lt;br&gt;
Liron.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>rust</category>
      <category>haskell</category>
      <category>functional</category>
    </item>
    <item>
      <title>Type thinking P1 - Types </title>
      <dc:creator>liron_hazan</dc:creator>
      <pubDate>Tue, 28 Apr 2020 10:54:52 +0000</pubDate>
      <link>https://forem.com/lironn_h/type-thinking-p1-types-3j94</link>
      <guid>https://forem.com/lironn_h/type-thinking-p1-types-3j94</guid>
      <description>&lt;p&gt;Types addiction.. no "taxes" or bulshit ;)&lt;/p&gt;

&lt;p&gt;Prologue: This post is the 1st of a series of short posts on comparing type exists in Haskell to their Typescript &amp;amp; Rust equivalents. &lt;br&gt;
For the past few months I've been on/off learning Rust and Haskell and that's an excuse to practice while wondering some fun theoretical stuff..&lt;/p&gt;

&lt;p&gt;What are types?&lt;br&gt;
Type Theory? &lt;br&gt;
What's HKT (Higher kind types) - typing types ?&lt;/p&gt;
&lt;h1&gt;
  
  
  Types as explained by the Haskell perspective:
&lt;/h1&gt;

&lt;p&gt;Instead of pasting the wikipedia explanation of a type system, let's review the Haskell like explanation from learnyouhaskell: &lt;/p&gt;

&lt;p&gt;" A type is a kind of label that every expression has. It tells us in which category of things that expression fits. The expression True is a boolean, "hello" is a string, etc. "&lt;/p&gt;
&lt;h1&gt;
  
  
  Type Theory 
&lt;/h1&gt;

&lt;p&gt;My guess is that I'm NOT gonna be an expert in Category, Types or Sets theories, but learning in high level and slowly deeping the knowledge is a good thing… and grasping types is an integral part of our logic.&lt;/p&gt;

&lt;p&gt;I recently discovered nLab (kind of a wiki for our topic but not exactly..) for future digging in theories, follows is the nLab definition of type theory:&lt;br&gt;
 &lt;br&gt;
" Explicitly, type theory is a formal language, essentially a set of rules for rewriting certain strings of symbols, that describes the introduction of types and their terms, and computations with these, in a sensible way."&lt;/p&gt;
&lt;h1&gt;
  
  
  HKT - higher kinded types
&lt;/h1&gt;

&lt;p&gt;Let's start demystifying the big words…&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;em&gt;kind&lt;/em&gt; is the type of a type constructor.&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;type constructor&lt;/em&gt; is a feature of a typed formal language that builds new types from old ones.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Higher Kinded Types are types who take types and construct a new type of them.&lt;br&gt;
In Haskell the kind of Maybe is * -&amp;gt; *.&lt;/p&gt;

&lt;p&gt;We don't have HKT on Typescript (not sure we need another abstraction, TS transpiled to JS anyway and it's not statically typed, it's gradually typed meaning we can still have untyped variable by using "any"..)&lt;/p&gt;

&lt;p&gt;My first thought of HKT was: "we have Generics" but that's wrong cause &lt;em&gt;Generics&lt;/em&gt; let us describe data types (values) and a Generic cannot use another Generic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;TypeA&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// I want to describe a type that is of Type A or null&lt;/span&gt;
&lt;span class="c1"&gt;// type maybe_A = TypeA&amp;lt;*&amp;gt; | null; // I can't&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;maybe_any_A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TypeA&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// I can do that but I guess the point of HKT will be lost&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;maybeA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TypeA&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// now the type of maybe A is a function that maybe returns A&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to feedback if you think I've got that wrong :)&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;p&gt; Bartosz Milewski Category Theory series&lt;br&gt;
 Dr. Erik Meijer - Functional Programming Fundamentals (C9)&lt;br&gt;
 &lt;a href="https://ncatlab.org/nlab/show/type+theory"&gt;https://ncatlab.org/nlab/show/type+theory&lt;/a&gt;&lt;br&gt;
&lt;a href="https://gist.github.com/gcanti/2b455c5008c2e1674ab3e8d5790cdad5"&gt;https://gist.github.com/gcanti/2b455c5008c2e1674ab3e8d5790cdad5&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cheers,&lt;br&gt;
Liron.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>haskell</category>
      <category>rust</category>
      <category>functional</category>
    </item>
    <item>
      <title>Micro-interactions in Angular</title>
      <dc:creator>liron_hazan</dc:creator>
      <pubDate>Fri, 21 Feb 2020 07:23:32 +0000</pubDate>
      <link>https://forem.com/lironn_h/micro-interactions-in-angular-30ai</link>
      <guid>https://forem.com/lironn_h/micro-interactions-in-angular-30ai</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i2N6vqwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1128/1%2AfIALx1aL1e_ObzMwDP23jw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i2N6vqwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1128/1%2AfIALx1aL1e_ObzMwDP23jw.gif" alt="Alt ng-micro-interact"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey Everyone, this is actually my first post in dev.to :) &lt;/p&gt;

&lt;p&gt;I want to share with you a tiny library I wrote for easily adding micro-interactions into your Angular app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ng-micro-interact-demo.stackblitz.io/"&gt;HIT THE DEMO&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My lib is based on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API"&gt;Web Animations&lt;/a&gt; ( experimental API ) which is one of the most performant ways of making animations for the web.&lt;/p&gt;

&lt;p&gt;To use ng-micro-interact in your Angular project do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install: &lt;code&gt;npm i ng-micro-interact&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Import &lt;code&gt;NgMicroInteractModule&lt;/code&gt; into your consumer module.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the &lt;code&gt;ngMicroInteract&lt;/code&gt; directive on any element you want to interact with.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading this post!&lt;br&gt;
I will really appreciate if you'll star the &lt;a href="https://github.com/LironHazan/ng-micro-interact"&gt;repo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>animations</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
