Chris's Wiki :: blog/programming/SomeOverloadingViews Commentshttps://utcc.utoronto.ca/~cks/space/blog/programming/SomeOverloadingViews?atomcommentsDWiki2018-05-27T21:04:49ZRecent comments in Chris's Wiki :: blog/programming/SomeOverloadingViews.By Ben C on /blog/programming/SomeOverloadingViewstag:CSpace:blog/programming/SomeOverloadingViews:d10200f83429f07e218bbef2e3f06879e27c750fBen C<div class="wikitext"><p>Operator overloading in Haskell is rather nice. It is a functional language but I don't think that's necessary for any of these features.</p>
<ul><li>You can define new operators, for example <code>@@</code>. This avoids having to twist the meaning of
an existing operator to do something completely different. (You can use Unicode symbols if you want to avoid ugly combinations of ASCII symbols!)<p>
</li>
<li>Overloading operators (and functions) is limited to type classes (like C++
concepts). For example, if you want to overload <code> + </code> for a type it needs to
be an instance of the Num typeclass - which requires you to implement at
least a few other functions and operators including <code> * </code> and <code>abs</code>.<p>
</li>
<li>You can use an operator as a prefix function by putting it in brackets,
thus <code> (*) 2 3 </code> and <code> 2 * 3 </code> are equivalent.<p>
</li>
<li>You can use a function of two arguments as an infix operator by putting
it in backticks, thus <code> foo 1 2 </code> and <code> 1 `foo` 2 </code> are equivalent.</li>
</ul>
</div>2018-05-27T21:04:49ZBy Dave on /blog/programming/SomeOverloadingViewstag:CSpace:blog/programming/SomeOverloadingViews:ed47a803a6da174ca9a4d5a21a51721098081654Dave<div class="wikitext"><p>I believe most languages allow whitespace between the object and the method call:</p>
<p>For example, in python:</p>
<pre>
>>> x=[4,1,2]
>>> x . sort()
>>> x
[1, 2, 4]
</pre>
</div>2018-05-12T23:38:26ZFrom 78.58.206.110 on /blog/programming/SomeOverloadingViewstag:CSpace:blog/programming/SomeOverloadingViews:583149f444e0a784b997ef0f4499aba92f7a762cFrom 78.58.206.110<div class="wikitext"><blockquote><p>leaving <code>*</code> available for scalar multiplication</p>
</blockquote>
<p>...by which I meant elementwise multiplication.</p>
</div>2018-05-12T16:50:48ZFrom 78.58.206.110 on /blog/programming/SomeOverloadingViewstag:CSpace:blog/programming/SomeOverloadingViews:825a0431c399161bd9739c099b7b009399be6992From 78.58.206.110<div class="wikitext"><blockquote><p>Matrix multiplication is not commutative, for example, but I don't think many people would argue against using * for it in a programming language.</p>
</blockquote>
<p>Oh, the arguments about matrix multiplication have actually gone to the point that Python now provides an <code>@</code> operator <em>just</em> for that purpose, leaving <code>*</code> available for scalar multiplication. See <a href="https://www.python.org/dev/peps/pep-0465/">PEP-465</a>.</p>
</div>2018-05-12T12:13:06Z