Skip to content

Commit 0293ea7

Browse files
committed
We need const default bodies for non-breaking changes
1 parent f33fe7a commit 0293ea7

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

const-generic-const-fn-bounds.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ const fn new<T: ?const Trait>(t: T) -> Foo<T> {
176176
}
177177
```
178178

179+
## `const` default method bodies
180+
181+
Trait methods can have default bodies for methods that are used if the method is not mentioned
182+
in an `impl`. This has several uses, most notably
183+
184+
* reducing code repetition between impls that are all the same
185+
* adding new methods is not a breaking change if they also have a default body
186+
187+
In order to keep both advantages in the presence of `impl const`s, we need a way to declare the
188+
method default body as being `const`. The author of this RFC considers prepending the default body's
189+
method signature with `const` to be the most intuitive syntax.
190+
191+
```rust
192+
trait Foo {
193+
const fn bar() {}
194+
}
195+
```
196+
197+
While this conflicts with future work ideas like `const` trait methods or `const trait` declarations,
198+
these features are unnecessary for full expressiveness as discussed in their respective sections.
199+
179200
# Reference-level explanation
180201
[reference-level-explanation]: #reference-level-explanation
181202

@@ -469,27 +490,6 @@ fn foo<T: const Bar>() -> i32 {
469490
Which, once `const` items and array lengths inside of functions can make use of the generics of
470491
the function, would allow the above function to actually exist.
471492

472-
## `const` default method bodies
473-
474-
Trait methods can have default bodies for methods that are used if the method is not mentioned
475-
in an `impl`. This has several uses, most notably
476-
477-
* reducing code repetition between impls that are all the same
478-
* adding new methods is not a breaking change if they also have a default body
479-
480-
In order to keep both advantages in the presence of `impl const`s, we need a way to declare the
481-
method default body as being `const`. The author of this RFC considers prepending the default body's
482-
method signature with `const` to be the most intuitive syntax.
483-
484-
```rust
485-
trait Foo {
486-
const fn bar() {}
487-
}
488-
```
489-
490-
While this conflicts with other future work ideas like `const` trait methods or `const trait` declarations,
491-
these features are unnecessary for full expressiveness as discussed in their respective sections.
492-
493493
# Unresolved questions
494494
[unresolved-questions]: #unresolved-questions
495495

0 commit comments

Comments
 (0)