Skip to content

Commit

Permalink
Deep constness does not apply only through the const keyword
Browse files Browse the repository at this point in the history
Where the type system expresses const vs mutable, the const keyword can be ignored _for view types_. Slice/SliceMut are the canonical example of this.
  • Loading branch information
danakj authored Aug 9, 2023
1 parent 5fbdebb commit 786c6f0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion PRINCIPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,16 @@ This library is an experiment and not intended for use. See the
* Native arrays can't be bounds-checked, and decay to pointers, making them
a bit invisible. A library type has the same overheads, unless it
explicitly chooses to provide more.
1. Deep constness. Const methods do not mutate state in visible ways.
1. Deep constness. Const methods of owning types do not mutate state in visible ways.
* No const methods return non-const pointers or references.
* No const methods call non-const methods through pointers or references.
* Reference/view types follow the same, or else provide clear const vs mutable
access. SliceMut allows const methods that act in mutable ways on the
container because it expresses mutability vs Slice in a clear way in the
type system. This compromise is required to allow receiving lvalue and rvalue
refences (as `const&`) without forcing multiple overloads onto library users
or without receiving by value ([which is more expensive](
https://danakj.github.io/2023/06/05/not-generating-constructors.html)).
* No `const_cast` usage.
* State in members marked `mutable` does not escape the class from const
methods.
Expand Down

0 comments on commit 786c6f0

Please sign in to comment.