内容简介:Lately, I’ve been hacking on the next version ofThe weird situation is aboutNow imagine you want a trait to move someone or something around:
Lately, I’ve been hacking on the next version of luminance
, luminance-0.40. It should be out “soon-ish”
but in the meantime, I’ve been struggling a bit with some highly and strongly typed code. I want to share something interesting I discovered with rustc
. Especially, I haven’t seen a mention of that property in the book
, so I’m happily sharing.
- What is a super trait?
- A small disgression to Haskell land
What is a super trait?
The weird situation is about super traits
. A super trait is a trait that must be implemented for another trait to be usable, because it’s relied on. Traits can be thought of as constraints
, so a super trait is a bit like a dependency
when implementing a trait, and an implication
when using a trait on which a super trait is declared. A trait can have zero to several super traits (added with the +
operator). For instance, imagine you have a trait Alive
:
trait Alive { fn get_health(&self) -> Health; }
Now imagine you want a trait to move someone or something around:
trait Move: Alive { fn go(&mut self, direction: Direction); }
Here, you can see that:
-
Move
requires to implementAlive
, because it’s a super trait. It’s a dependency . -
Because
Move
requiresAlive
,Alive
is implied when you useMove
. Indeed, that would be redundant to annotate a typeT: Move + Alive
, because an instance (implementor) forMove
cannot exist withoutAlive
to be implemented as well.
So now that we understand what super traits are, let’s get to the weird stuff.
The weird stuff
When you implement a trait which has a super trait, do you think that:
- Your implementation is valid when the super trait is implemented ? After all, you could simply assume it is implemented. If it’s not, instances of your trait won’t be pickable.
- Or your implementation requires the super trait to be implemented?
The distinction is important. (1.) doesn’t require rustc
to prove when implementing a trait
that the super trait is implemented. That will be required when using the trait. With (2.), rustc
will have to prove that the super trait is, first, implemented, before even considering the implementation of the trait you’re making.
Rust currently uses (2.). If you impl Move for Foo
, impl Alive for Foo
must be in scope for that implementor to be possible.
But… it would be interesting to actually have (1.). Imagine that you want to implement Move
for something a bit complex, like Creature<T>
, but not all Creature<T>
are alive. Only a subset of them, and you can’t tell exactly when — i.e. you just cannot assume anything about T
. So what are you going to write?
impl<T> Move for Creature<T> { fn go(&mut self, direction: Direction) { // … } }
This code will not compile, because you haven’t implemented Alive for Creature<T>
. Remember that the trait solver requires to prove super traits. However, and this is where all the interesting / weird stuff happens:
impl<T> Move for Creature<T> where Self: Alive { fn go(&mut self, direction: Direction) { // … } }
This compiles. It compiles because the where
clause tells rustc
that your implementor will be valid
if used with Creature<T>: Alive
. The distinction is really subtle, but is, to my opinion, very powerful. With the where
clause, you state that Move
is implemented for any Creature<T>
that is also Alive
, but you don’t require them all to be Alive
! You could implement Alive
for a bunch of creatures, like Creature<Vampire>
and Creature<CloudDog>
.
So, I remember having read somewhere (maybe in some Rust book, but I’m not quite sure) that the where Self: _
clause was not really useful, but in our case, you can see that it allows to express a completely different semantics.
You could also have used Creature<T>: Alive
in place of Self: Alive
, as here, Self = Creature<T>
.
A small disgression to Haskell land
In Haskell, that code requires to use UndecidableInstances
. I don’t know exactly
why, but GHC states that the constraint ( Alive (Creature a)
) is no smaller than the instance head ( Move (Creature a)
), and this is not permitted, as being undecidable. Enabling the UndecidableInstances
GHC extension will make it possible to compile:
class Alive a where getHealth :: a -> Health -- The super class is declared on the left side of the => (the parenthesis are -- optional, but I’m used to put them all the time, as they are required when you -- have more constraints). class (Alive a) => Move a where go :: Direction -> a -> a -- This instance requires UndecidableInstances to compile. instance (Alive (Creature a)) => Move (Creature a) where go = -- … instance Alive (Creature Vampire) where getHealth = -- … instance Alive (Creature CloudDog) where getHealth = -- …
I’m not quite sure why this very exact
situation requires UndecidableInstances
though. In this case, it seems fine.
I hope you’ll have learned something with this small yet fun type theory candy. Keep the vibes!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
C#图解教程
索利斯 (Daniel M.Solis) / 姚琪琳、苏林、朱晔 / 人民邮电出版社 / 2013-7-1 / CNY 89.00
本书是广受赞誉的C# 图解教程的最新版本。作者在本书中创造了一种全新的可视化叙述方式,以图文并茂的形式、朴实简洁的文字,并辅以大量表格和代码示例,全面、直观地阐述了C# 语言的各种特性。新版本除了精心修订旧版内容外,还全面涵盖了C# 5.0 的新增特性,比如异步编程、调用者信息、case 表达式、带参数的泛型构造函数、支持null 类型运算等。通过本书,读者能够快速、深入理解C#,为自己的编程生涯......一起来看看 《C#图解教程》 这本书的介绍吧!