Doesn’t every function has its own type already? Like fn () {fn_name}
But currently there is no way to name it in code, so you cant add methods or impl traits for it. But what if we could?
I think it would be possible to name it just exactly like the function itself, if it is used in type’s context. Like this:
fn foo<T>() {}
fn main() {
let f: foo<i32> = foo::<i32>;
}
This currently errors:
error[E0573]: expected type, found function `foo`
--> src\lib.rs:4:12
|
4 | let f: foo<i32> = foo::<i32>;
| ^^^^^^^^ not a type
It works exactly the same way for structs, right?
struct Foo<T> {
inner: T,
}
fn main() {
let foo: Foo<i32> = Foo::<i32> {
inner: 0,
};
}
If this worked for fns, it would be possible to associate constants same way as for structs:
fn foo<T>() {}
impl foo<i32> {
const FOO: i32 = 0;
}
trait HasConst {
const CONST: i32;
}
impl<T> HasConst for foo<T> {
const CONST: i32 = 123;
}
But I guess this would be a breaking change since this would forbid modules with same name as function.