Alle boeken

Professioneel

Apps Over Coach Inloggen Begin met lezen

Computer Science · Begrippenlijst

Wat is Object safety?

Definition 8.8 University Computer Science — Year 2 · Hoofdstuk 8 — Traits and Generics

A trait is object safe, or dyn compatible, if dyn Trait can be formed: its methods can be called through a vtable. Methods that return Self by value, or have type parameters of their own, break this (the caller does not know the size of Self; a generic method would need one table entry per type argument), unless they are excluded with where Self: Sized.

Static and dynamic dispatch. Above, a generic is copied per type and each copy calls its methods directly, where they can be inlined. Below, a function on trait objects exists once, and each call goes through the vtable of the value at hand.
Static and dynamic dispatch. Above, a generic is copied per type and each copy calls its methods directly, where they can be inlined. Below, a function on trait objects exists once, and each call goes through the vtable of the value at hand.
Lees in het hoofdstuk →