内容简介: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!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
第二曲线:跨越“S型曲线”的二次增长
[英]查尔斯·汉迪(Charles Handy) / 苗青 / 机械工业出版社 / 2017-6 / 49.00
S型曲线是每个组织和企业在预测未来时一定会参考的工具,一切事物的发展都逃不开S型曲线(“第一曲线”)。 然而,从公司组织、企业治理、市场的变化,到个人职业发展、社会人际关系以及未来的教育与社会价值,多维度地探讨这个世界需要重新以不同的角度来思考问题,不能够总是停留在“第一曲线”的世界。 如果组织和企业能在第一曲线到达巅峰之前,找到带领企业二次腾飞的“第二曲线”,并且第二曲线必须在第一曲......一起来看看 《第二曲线:跨越“S型曲线”的二次增长》 这本书的介绍吧!
RGB转16进制工具
RGB HEX 互转工具
正则表达式在线测试
正则表达式在线测试