

A postscript on traits and impls
source link: http://smallcultfollowing.com/babysteps/blog/2012/10/04/a-postscript-on-traits-and-impls/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A postscript on traits and impls
Oct 4, 2012
I was thinking more about type classes as I walked down the street. In my prior post I wrote that the rules I proposed resulted in a system where traits loosely fit the following Haskell template:
class C self a ... z | self -> a ... z where ...
However, I gave two caveats. The first was that due to subtyping we
cannot say that one type precisely determines another, but only that
it puts a bound. The second was that, in any given impl, the value of
a ... z
may be a type parameter which does not appear in the self
type. I think I understated the importance of this second caveat.
For example, consider the example I gave for simulating overloading:
trait Add<R,S> { pure fn add(&self, rhs: &R) -> S; }
trait IntRhs<S> { pure fn add_to_int(&self, lhs: int) -> S; }
impl<S, R: IntRhs<S>> int: Add<R, S> { ... }
This impl declaration essentially says “when self
is int
, the type
parameter R
may be any type which implements IntRhs
”. Moreover,
in this case, the self
type does not constrain the parameter S
at
all—that constraint is derived purely from R
.
In other words, while overloading-freedom does mean that the impl
which will be used is purely determined by self
, it does not mean
that self
alone determines the value of all the other trait
parameters, as my Haskell analogy implied. It’s more accurate to say
that the self
type determines a (possibly empty) set of bounds that
will be imposed on the other type parameters. These bounds can take
the form of subtyping bounds (lower- or upper-bounds, or both) or
trait bounds.
Recommend
-
9
I had intended to end this blog series with the previous post, but my own personal yak-shaving led to wanting to add one more feature (for now) to Graphvix , and so I wanted to write it up for any readers I...
-
11
Refining traits and impls Oct 4, 2012 Currently, the Rust compiler accepts all manner of trait, impl, and bound declarations. In fact, it accepts plenty of declarations that later phases of the compiler are not sophisti...
-
7
Swallowing the elephant (postscript: reader emails) Aug 3, 2018 It was due to a mixture of intention and laziness that I didn’t add the ability for people to comment on blog post...
-
7
The story of ispc: postscript (part 12) Apr 30, 2018 Excellence withers without an adversary: the time for us to see how great it is, how much its force, is when i...
-
14
Postscript Type 1 Fonts B
-
5
How-To Adobe is dropping PostScript Type 1 font support. Be prepared for the change The fontpocalypse is coming. By
-
8
Adobe 停止支持 PostScript Type 1 字体 WinterIsComing (31822)发表于 2021年05...
-
4
Swallowing the Elephant (Part 12): A Postscript On Disk Bandwidth Aug 7, 2021 At the ostensible end of these updates about pbrt-...
-
8
Gilles Deleuze Postscript on the Societies of Control 1. Historical Foucault located the disciplinary...
-
4
Barcode Writer in Pure PostScript Project maintained by Terry Burton Hosted on GitHub Pages Barco...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK