Skip to content

Namespaced enums are weird in regard to generic paramters #22486

Closed
@Kimundi

Description

@Kimundi

Enum variants are namespaced now, but applying generic paramters to them in expressions still works as before, which means the correct way to type annotate a Option in a expression is to do this:

enum Option<T> {
    Some(T),
    None
}
let x = Option::Some::<u32>(1);

However, this is inconsistent with other namespaced constructs, like this:

struct Foo<T>(T);
impl<T> Foo<T> { fn new(t:T) -> Foo<T> { Foo(t) } }

let x = Foo::<u32>::new(1);

// With UFCS
let f = Option::<u32>::and as fn(Option<u32>, Option<u32>) -> Option<u32>;

Generally, type parameters of an item are supposed to go on that item in a path, but for enum variants this is not the case.

As a backward compatible solution to this, enum variants could be made to work similar to associated items of traits:

Option::Some      // Generic item fn<T>(T) -> Option<T>
Option::<T>::Some // Concrete instantiated item fn(T) -> Option<T>

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions