<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="13765" data-post-id="13765">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ibgib" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/120/2176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ibgib
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="OvermindDL1" data-post="50" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>I’m curious, what is special about C# generics?  They seem extremely limiting compare to what I’m used to in other languages (C++, Scala, OCaml (Functors especially), even lacking features compared to Rusts’s limited form of Generics)?</p>
</blockquote>
</aside>
<p>The effect of generics on me is a personal remark. I was not born a programmer as my brothers were (since they were 5 or so). I dabbled in assembly and C but didn’t start going full at it until I was using Delphi. In Delphi 5.0 at the time (early 2000s), there weren’t generics yet. They may have existed in other languages. So in my building of my multi-threaded transcription program (I was typing neurologic transcription at the time and thus developing/dogfooding it for myself), I remember having to explicitly write out many list classes, events, etc., that were to enable strongly typing things. This carried over in the earliest version of C#. When generics were introduced, they significantly improved the expressive power of the language and allowed me to cull my code greatly - especially in combination with reflection (runtime introspective programming).</p>
<p>As for compared to other languages, I’ve looked a little into the OCaml functors <a href="https://realworldocaml.org/v1/en/html/functors.html" rel="noopener nofollow ugc">here</a>, and I have to say that it seems to be a strange (broad?) comparison when comparing generics in an OOP language like C# to Functors in an FP language such as OCaml. From what I can grok there, it appears that the modules are like interfaces and that the functors are used to “build up” complex dynamic functions, much like a compound currying mechanism. This behavior in C# would like be implemented using base classes which implement interfaces. Then, using generics, you could pass around strongly-typed references via the interface/class. You can also declare constraints on the generics when doing this as well.</p>
<p>So for the simpler <code>Increment</code> example, you can actually pass around generic anonymous functions, such as <code>Func&lt;int, int&gt;</code>, but  to represent a function that takes a single <code>int</code> argument and returns an <code>int</code>, such as (oh goodness, from C# memory…it’s been a couple years now!): <code>var inc = x =&gt; return x + 1;</code>. To do a closure around an anonymous lambda function I believe would just be to set a variable before the declaration:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">var y = 2;
var inc2 = x =&gt; return y + 1;
</code></pre>
<p>But my memory is hazy on the scoping rules for them. It would be unwieldy to pass around more curry-like functions, such as a <code>Func&lt;Func&lt;int, int&gt;, int&gt;</code> (but I have done it).</p>
<p>Also, from reading through that page, it mentions the required explicit module type attribution. My gut feeling is that this has to do with covariance and contravariance? (oh we’re digging deep for vocab now). Generics didn’t <em>initially</em> have this capability, but at some point it was introduced and allowed for even more powerful type inference in the compiler. This makes me want to search for an example in the old ibgib codebase, because I’m sure I used it. I had built the entire codebase on a single interface (going against standard C# naming conventions unfortunately, but I had to do it): <code>Task&lt;ib&gt; gib(ib gib)</code>. As I’ve learned more about FP, it turns out I was just about trying to shove FP into C# and I didn’t realize it at the time! <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<p>EDIT: Looking at the mathematical functors on the <a href="https://en.wikipedia.org/wiki/Functor" rel="noopener nofollow ugc">all-knowing wikipedia</a>, it does indeed look like that is why <a href="https://en.wikipedia.org/wiki/Functor#Covariance_and_contravariance" rel="noopener nofollow ugc">co/contravariance</a> was introduced to generics in C#. I always would get confused on which one is which, but the practical side was that you could define generic types using the keywords <code>in</code> and <code>out</code>, depending on how a type was being used.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="13765" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/elixir-vs-c/670/52">Post #51</a>
	                </div>
	            </div>
              <div id="likers-container-13765" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="13765"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #51"></div>
  </section>
</div>
    <div class="postbit" id="13772" data-post-id="13772">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Onor.io" data-post="51" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/onor.io/48/960_2.png" class="avatar"> Onor.io:</div>
<blockquote>
<p>Perhaps you can enlighten me about why parameterized modules (and Functors)<br>
are such a killer feature in Ocaml?  I have to confess that like Haskell’s<br>
Turing Complete Type System I never quite understood why it was such an<br>
advantage to have. I know there were a few other folks who crossed the gap<br>
from OCaml to F# who also missed parameterized modules but I could never<br>
quite figure out what sort of code parameterized modules would enable one<br>
to create and why their lack in F# was such a pain point for those folks<br>
who’d used OCaml before.</p>
</blockquote>
</aside>
<p>Hehe, this is a fun subject, but I will try to keep it succinct.  ^.^</p>
<p>First of all, OCaml’s modules are what enable full Higher Polymorphic Types.  Like Haskell’s Higher Kinded Types (which allow typeclasses and more) HPT’s solve the same problems but in a different way.  The parameterized and first-class modules in OCaml are what allow passing strong but ‘unknown’ types ‘through’ functions, which immediately makes obvious that you can emulate HKT’s that way, so it has that ability, <em>BUT</em> instead of requiring whole program type scanning, like Haskell does (and is the <strong>major</strong> source of its slow compiling), the HPT’s in OCaml only need to know about what is at the point where it is used, thus enabling <strong>significantly</strong> faster compiling for the same features.  With parameterized modules, even ignoring the HPT’ness of their immense capabilities, you can emulate Haskell’s typeclasses.  The usual example is to reimplement the Haskell’y <code>show</code> typeclass, so let me do that here, first lets define the ‘typeclass’ type:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">module type Show = sig
  type t
  val show : t -&gt; string
end
</code></pre>
<p>This is just a type of a module, any module that has a type named <code>t</code> (which is the common ‘module-level’ type name by standard) and has a function in it called <code>show</code> that takes the <code>t</code> type of the module and returns a string.</p>
<p>Now let’s define a function that uses this type to get the string of whatever:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">let show ( S : Show ) x = S.show x
</code></pre>
<p>Now immediately you’d be thinking (if you know Haskell), “Well why not just use a HKT here and pass the types based on ‘x’ directly?!?”, well that is because OCaml is made to be fast, both in compiling and execution, so we need to ‘decorate’ the type.  The <code>{ S : Show }</code> is 'destructuring (just like in elixir matching) the passed in First-Class module to give it a name that we can use, just like it was a full module.  We then call the <code>show</code> function that is defined in that module, passing to it <code>x</code>, which will not compile if <code>x</code> is a different type from <code>S.t</code>.  So we could do something like this:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">print_endline ("Show an int: " ^ show (module struct type t = int; let show x = string_of_int x end : Show) 5)
</code></pre>
<p>Here I pass in a module to show, that I define inline (yes you can even define modules inline), that can handle the int of <code>5</code> that I pass in, however this is wordy, so as per the common OCaml examples let’s define a few useful global modules that fulfill <code>Show</code>:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">module Show_int : Show = struct
  type t = int
  let show = string_of_int
end

module Show_float : Show = struct
  type t = float
  let show = string_of_float
end

module Show_list ( S : Show ) : Show = struct
  type t = S.t list
  let show = string_of_list S.show
end
</code></pre>
<p>Now we can use it like:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">print_endline (" Show an int: " ^ show Show_int 5);
print_endline (" Show a float : " ^ show Show_float 1.5);
print_endline (" Show a list of ints : " ^ show Show_list(Show_int) [1; 2; 3]);
</code></pre>
<p>Now you can call <code>show</code> on any type that has an appropriate module defined, and you can pass the module down however deep through many functions.  As you can see, <code>Show_list</code> is a function here, since it will in turn take a module of whatever can display its internal type.  This gives you the full power of typeclasses in Haskell, but without the exponential compilation cost (it is O(1) here!), however it does mean having to pass a handler around, what is usually called in OCaml circles as a ‘Witness module’ through the functions.  This is where people get that OCaml’s modules can do what typeclasses can (and more) but it is a bit more wordy as you have to carry the witness around.</p>
<p><em>However</em>, a soon-coming OCaml version has an accepted feature called ‘Implicit Modules’, let me demonstrate:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">implicit module Show_int = struct
  type t = int
  let show = string_of_int
end

implicit module Show_float = struct
  type t = float
  let show = string_of_float
end

implicit module Show_list { S : Show } = struct
  type t = S . t list
  let show = string_of_list S.show
end
</code></pre>
<p>So, just added the implicit keyword was all (and I do not need to force type them as this will work with any implicit that fulfills the type), in addition to making the argument on the Show_list function take an implicit, an implicit argument is defined with <code>{}</code> instead of <code>()</code>.  To use it I just need to redefine my `show function as:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">let show { S : Show } x = S.show x
</code></pre>
<p>Now <code>S</code> passed in module is an implicit.  I can now use it like:</p>
<pre data-code-wrap="ocaml"><code class="lang-ocaml">print_endline (" Show an int: " ^ show 5);
print_endline (" Show a float : " ^ show 1.5);
print_endline (" Show a list of ints : " ^ show [1; 2; 3]);
</code></pre>
<p>The modules are looked up by the compiler first by the type the function wants (<code>Show</code> in this case) and any implicit module that fulfills this is eligible, then it tests the types in the module so that they are compatible, and it fills in at the call site which module is able to be used.</p>
<p>However, this would be a bit magical if done anywhere, and more costly, so to make a module eligible for this it has to be opened implicitly, which you normally open a module via <code>open ModuleName</code>, to add an implicit module to be able to be used for resolution this is done via <code>open implicit ModuleName</code>, and doing this could open a whole set of modules as well if <code>ModuleName</code> module included all the above defined implicit Show modules.  This needs to be opened in the scope of where <code>show</code> will be called, thus at the caller site.  If you have implicit functions that call implicit functions and so forth, each implicit function needs to define its implicit parameter, thus only the original base call site require the implicit loaded (and since the call site knows what the type is then that is trivial).</p>
<p>The implicit modules where modeled on Scala’s implicit classes.  It allows using Witness modules via a substantially more simple syntax, and functions that use them only define the signature of a module they want to accept, nothing more.</p>
<p>But with all these features, OCaml modules are able to emulate Haskell typeclasses, Haskell HKT’s, and even more, all without compromising the ability for the compiler to optimize or compromising compile time at all.  This is why OCaml is one of the fastest compiling native languages out even while doing optimizations that make it rival C++.</p>
<p>F#, for comparison, require .NET, and uses a <em>LOT</em> of dynamic dispatch, which will always make it slower than OCaml itself as the OCaml compiler gets rid of every dynamic dispatch that it can.  To do dynamic dispatch in OCaml actually takes more work than just doing things correctly, consequently such code in OCaml is never used unless absolutely necessary.  The design of the language encourages doing things the right way.  <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Also, don’t you love that even using ‘implicit’ things in OCaml still requires an explicit call to state that you are doing so?  <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="13772" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/elixir-vs-c/670/53">Post #52</a>
	                </div>
	            </div>
              <div id="likers-container-13772" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="13772"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #52"></div>
  </section>
</div>
    <div class="postbit" id="13774" data-post-id="13774">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="ibgib" data-post="52" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/48/2176_2.png" class="avatar"> ibgib:</div>
<blockquote>
<p>The effect of generics on me is a personal remark. I was not born a programmer as my brothers were (since they were 5 or so). I dabbled in assembly and C but didn’t start going full at it until I was using Delphi. In Delphi 5.0 at the time (early 2000s), there weren’t generics yet. They may have existed in other languages. So in my building of my multi-threaded transcription program (I was typing neurologic transcription at the time and thus developing/dogfooding it for myself), I remember having to explicitly write out many list classes, events, etc., that were to enable strongly typing things. This carried over in the earliest version of C#. When generics were introduced, they significantly improved the expressive power of the language and allowed me to cull my code greatly - especially in combination with reflection (runtime introspective programming).</p>
</blockquote>
</aside>
<p>Ah true, I could see that.  I started programming with assembler so the concept of typing was… different there.  ^.^</p>
<aside class="quote no-group" data-username="ibgib" data-post="52" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/48/2176_2.png" class="avatar"> ibgib:</div>
<blockquote>
<p>As for compared to other languages, I’ve looked a little into the OCaml functors here, and I have to say that it seems to be a strange (broad?) comparison when comparing generics in an OOP language like C# to Functors in an FP language such as OCaml. From what I can grok there, it appears that the modules are like interfaces and that the functors are used to “build up” complex dynamic functions, much like a compound currying mechanism. This behavior in C# would like be implemented using base classes which implement interfaces. Then, using generics, you could pass around strongly-typed references via the interface/class. You can also declare constraints on the generics when doing this as well.</p>
</blockquote>
</aside>
<p>Eh, kind of, you can see my post above for more details, but basically a Functor makes to static dispatch.  To do, as you said, in C# using base classes, that means you will have dynamic dispatch.  OCaml can resolve at compile time the types of everything and thus dynamic dispatching is not needed and can in fact be <em>tightly</em> optimized, this is not something that .NET can do because it only optimizes out one function and even then only for inlining purposes, it cannot know what a type will be of something as it is called.  For example .NET a List that is defined to, say, <code>int</code> for its internal type, it cannot optimize for this as internally it is just storing boxed types and working on them (in reality .NET does optimize ‘somewhat’ for primitives, mostly for memory but not necessarily speed except to just remove boxing costs, and it does not for compound types at all, where OCaml does in all cases).</p>
<aside class="quote no-group" data-username="ibgib" data-post="52" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/48/2176_2.png" class="avatar"> ibgib:</div>
<blockquote>
<p>Also, from reading through that page, it mentions the required explicit module type attribution. My gut feeling is that this has to do with covariance and contravariance?</p>
</blockquote>
</aside>
<p>It does but in a soon-coming OCaml version this has an enhancement to fix that, as detailed in my prior post.  <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>And nope, nothing to do with covariance or contravariance, a Functor can handle those both, as well as other things even (want to make sure an ‘int’ type is only between 3 and 10, you could do that!).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="13774" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/elixir-vs-c/670/54">Post #53</a>
	                </div>
	            </div>
              <div id="likers-container-13774" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="13774"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #53"></div>
  </section>
</div>
    <div class="postbit" id="13779" data-post-id="13779">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ibgib" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/120/2176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ibgib
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>First off, great information on functors! Definitely a nice additional example to the page I referenced. <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="OvermindDL1" data-post="50" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>I’m curious, what is special about C# generics?  They seem extremely limiting compare to what I’m used to in other languages (C++, Scala, OCaml (Functors especially), even lacking features compared to Rusts’s limited form of Generics)?</p>
</blockquote>
</aside>
<aside class="quote no-group" data-username="OvermindDL1" data-post="54" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>Eh, kind of, you can see my post above for more details, but basically a Functor makes to static dispatch.  To do, as you said, in C# using base classes, that means you will have dynamic dispatch.  OCaml can resolve at compile time the types of everything and thus dynamic dispatching is not needed and can in fact be tightly optimized, this is not something that .NET can do because it only optimizes out one function and even then only for inlining purposes, it cannot know what a type will be of something as it is called.  For example .NET a List that is defined to, say, int for its internal type, it cannot optimize for this as internally it is just storing boxed types and working on them (in reality .NET does optimize ‘somewhat’ for primitives, mostly for memory but not necessarily speed except to just remove boxing costs, and it does not for compound types at all, where OCaml does in all cases).</p>
</blockquote>
</aside>
<p>Stepping back, I would say now we’re talking about speed optimizations and I was referring to the evolution of generics per the “<em>extremely</em> limiting” aspect. <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>
<p>With regards to dynamic vs static dispatch, now we’re getting into the oddness of the comparison in the first place. The term <code>type</code> has two different meanings here when we refer to what “types” are known at compile time, yes? In OCaml, types are more restricted to data structures, whereas in any OOP language, types refer to both the primitive types that you mention, as well as the complex constructs that gives us the “Object” in OOP. And in this sense, C# does indeed know all of the types and interfaces at compile time as per the constraints that I mentioned.</p>
<p>Whether one is faster than the other, I am totally not an optimization expert…so I will <img src="https://forum.elixirforum.com/images/emoji/apple/bow.png?v=15" title=":bow:" class="emoji" alt=":bow:" loading="lazy" width="20" height="20"> to your knowledge on that one! <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<p>And btw, this has definitely gotten me to look more closely at syntax and OCaml constructs, so now I need to check that Bucklescript thread again, planting more seeds for a better front end for ibGib! <img src="https://forum.elixirforum.com/images/emoji/apple/laughing.png?v=15" title=":laughing:" class="emoji" alt=":laughing:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="13779" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/elixir-vs-c/670/55">Post #54</a>
	                </div>
	            </div>
              <div id="likers-container-13779" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="13779"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #54"></div>
  </section>
</div>
    <div class="postbit" id="13786" data-post-id="13786">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="OvermindDL1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/OvermindDL1/120/2677_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  OvermindDL1
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="ibgib" data-post="55" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/48/2176_2.png" class="avatar"> ibgib:</div>
<blockquote>
<p>Stepping back, I would say now we’re talking about speed optimizations and I was referring to the evolution of generics per the “extremely limiting” aspect. <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>I was comparing what Generics could do compared to OCaml modules (I’m not touching C++ templates, if you think my above OCaml post was long, you’ve seen nothing! ;-)), I was not elaborating on everything else modules could do, just based on ‘just’ generics and what they could do, they are horribly inefficiently done.  ^.^</p>
<aside class="quote no-group" data-username="ibgib" data-post="55" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/48/2176_2.png" class="avatar"> ibgib:</div>
<blockquote>
<p>With regards to dynamic vs static dispatch, now we’re getting into the oddness of the comparison in the first place. The term type has two different meanings here when we refer to what “types” are known at compile time, yes? In OCaml, types are more restricted to data structures, whereas in any OOP language, types refer to both the primitive types that you mention, as well as the complex constructs that gives us the “Object” in OOP. And in this sense, C# does indeed know all of the types and interfaces at compile time as per the constraints that I mentioned.</p>
</blockquote>
</aside>
<p>A class is a type sure, but there is a whole set of classes can that fulfill a given type, but accessing those class instances require dynamic dispatch, that is one of the main failings of heavy OOP systems in performance constrained code (personal experience especially, plenty of docs on google about this like <a href="http://www.dataorienteddesign.com/dodmain/node17.html" class="inline-onebox" rel="noopener nofollow ugc">What's wrong?</a> as an initial find, and I quote <code>Virtuals don't cost much, but if you call them a lot it can add up. aka - death by a thousand papercuts</code> when it is entirely unnecessary).</p>
<aside class="quote no-group" data-username="ibgib" data-post="55" data-topic="670">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/48/2176_2.png" class="avatar"> ibgib:</div>
<blockquote>
<p>And btw, this has definitely gotten me to look more closely at syntax and OCaml constructs, so now I need to check that Bucklescript thread again, planting more seeds for a better front end for ibGib! <img src="https://forum.elixirforum.com/images/emoji/apple/laughing.png?v=15" title=":laughing:" class="emoji" alt=":laughing:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>Lol, I really want an OCaml-&gt;Elixir backend written.  ^.^</p>
<p>OCaml is in a fuzzy place for me.  For ‘real work’ that will be hosting a long lived server that needs uptime, I’ll use Erlang/Elixir.  For ‘real work’ that is more usual programs I use either C++ or (much more often recently) Rust.  OCaml is more ‘fun’ for me, I enjoy programming in it, and it is certainly doable with <code>real-work</code>, I just never end up choosing it for <code>real-work</code>, but I only have very few good reasons (maybe only one) as to why I would not choose it (it still has a GC for example), where I have a <em>LOT</em> more reasons to not choose anything on the JVM or .NET systems for <code>real-work</code> (ew GC, ew OOP, etc…).</p>
<p>Rust I really have to espouse on again, it really is C++ done right, strongly typed, no GC, does not even need a GC as it uses proper semantics for handling <em>all</em> resources (not just memory), basically C++'s RAII baked in to the language with brilliant borrowing semantics.  Still not as functional as I would like, and I do fight with it a lot more (mostly with its macros, I have borrowing down solid), where OCaml, if I fight with it, it always shows me how I was wrong in the end, and I come away more enlightened.  ^.^</p>
<p>(Fighting with Rust on the other hand just leaves us both battered and me bugged at the lack of any form of higher types in it or anything like a C++ template system…)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="13786" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/elixir-vs-c/670/56">Post #55</a>
	                </div>
	            </div>
              <div id="likers-container-13786" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="13786"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>