Why are type parameters are not allowed on this type?

Thanatos

Attempting to compile the following,

fn do_it() -> Result<i32, u32> {
    Result::<i32, u32>::Ok(3)
}


fn main() {
    println!("{}", do_it());
}

results in:

./result_test.rs:2:14: 2:17 error: type parameters are not allowed on this type [E0109]
./result_test.rs:2     Result::<i32, u32>::Ok(3)
                                ^~~

Why are type parameters not allowed on this type?

This is a minimal example, my real-world example is a macro trying to return the following:

match $reader.$read_func() {
    Ok(n) => Result::<$read_type, LocalReadError>::Ok(n),
    Err(err) => Result::<$read_type, LocalReadError>::Err(
        LocalReadError::from(err)
    ),
}

$read_func is a function, $read_type is the return type of that function. (If I had a programmatic way to get that, I'd do so; I don't know how, so it's an arg…); as-is, I get the above error. If I remove the specification of the generic's parameters, type inteference complains that it can't figure out the type. (Because it ends up with Result<_, LocalReadError> in one branch of the match, and Result<$read_type, _> in the other? I'm not really sure. It says:

error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282]
    match $reader.$read_func() {
                  ^~~~~~~~~~~~

)

Note: The question about why type parameters are not allowed is answered below. Turns out this is not the cause of the "unable to infer enough type information". (read_func is a function, in my case, I'm passing a templated function, but forgetting the template arg, which can't be inferred.)

Paolo Falabella

this is actually an inconsistency with enums that was discussed but not considered important enough to block 1.0.

The working syntax to specify types is Result::Ok::<i32, u32>(3).

An enum works like something between a type (that would go with the syntax you were trying to write) and a namespace (and namespaces don't accept type parameters).

To demonstrate how enums are like namespaces, you can write:

use std::result::Result::*;

fn main() {
    println!("{:?}", Ok::<i32, u32>(3));
}

This namespacing aspect is a desirable property of enums, but moving type parameters where one would intuitively think they should be would make this type of code very awkward to write.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why sealed classes are not allowed to be generic type parameters?

From Dev

Why sealed classes are not allowed to be generic type parameters?

From Dev

Expressions depending on integer type parameters in type definitions in Julia are not allowed

From Java

Why type alias is allowed as name of variable?

From Dev

Why are we allowed to take the address of an incomplete type?

From Dev

Why is throwing a checked exception type allowed in this case?

From Dev

Why is class of T not allowed in a generic type?

From Dev

Why is an array of an unknown generic type allowed in Java?

From Dev

Why are flexible types not allowed in record type definitions?

From Dev

Why are incorrect type assignments allowed in Dart?

From Dev

Why is it allowed to convert a string type to byte slice?

From Dev

Why is an array of an unknown generic type allowed in Java?

From Dev

Why is class of T not allowed in a generic type?

From Dev

Why are parameters serialized on ShowViewModel<Type>?

From Dev

Are Generic Generic type parameters in C# allowed in some form

From Dev

are iOS block parameters somehow allowed to be specific subclasses of defined type

From Dev

Are Generic Generic type parameters in C# allowed in some form

From Dev

are iOS block parameters somehow allowed to be specific subclasses of defined type

From Dev

Why is Circular reference in struct of instance type not allowed but circular reference of static type allowed?

From Dev

Why am i allowed to pass an object of the bounding type of a generic type to a function, but not allowed to return it?

From Dev

Why do type parameters disappear in TypeScript?

From Dev

Why can associated constants not depend on type parameters?

From Dev

Java - Why cannot type parameters be in a catch clause?

From Dev

Why the gcc compiler allowed assigning value without giving data type?

From Dev

Haskell: Why aren't infix type constructors allowed?

From Dev

Why compilation allowed when expected is return type mismatch?

From Dev

Why isn't overloading with a different generic type as a parameter allowed?

From Dev

Why is double not allowed as a non-type template parameter?

From Dev

Java - Why is it allowed to have Variable Name as Type Name

Related Related

  1. 1

    Why sealed classes are not allowed to be generic type parameters?

  2. 2

    Why sealed classes are not allowed to be generic type parameters?

  3. 3

    Expressions depending on integer type parameters in type definitions in Julia are not allowed

  4. 4

    Why type alias is allowed as name of variable?

  5. 5

    Why are we allowed to take the address of an incomplete type?

  6. 6

    Why is throwing a checked exception type allowed in this case?

  7. 7

    Why is class of T not allowed in a generic type?

  8. 8

    Why is an array of an unknown generic type allowed in Java?

  9. 9

    Why are flexible types not allowed in record type definitions?

  10. 10

    Why are incorrect type assignments allowed in Dart?

  11. 11

    Why is it allowed to convert a string type to byte slice?

  12. 12

    Why is an array of an unknown generic type allowed in Java?

  13. 13

    Why is class of T not allowed in a generic type?

  14. 14

    Why are parameters serialized on ShowViewModel<Type>?

  15. 15

    Are Generic Generic type parameters in C# allowed in some form

  16. 16

    are iOS block parameters somehow allowed to be specific subclasses of defined type

  17. 17

    Are Generic Generic type parameters in C# allowed in some form

  18. 18

    are iOS block parameters somehow allowed to be specific subclasses of defined type

  19. 19

    Why is Circular reference in struct of instance type not allowed but circular reference of static type allowed?

  20. 20

    Why am i allowed to pass an object of the bounding type of a generic type to a function, but not allowed to return it?

  21. 21

    Why do type parameters disappear in TypeScript?

  22. 22

    Why can associated constants not depend on type parameters?

  23. 23

    Java - Why cannot type parameters be in a catch clause?

  24. 24

    Why the gcc compiler allowed assigning value without giving data type?

  25. 25

    Haskell: Why aren't infix type constructors allowed?

  26. 26

    Why compilation allowed when expected is return type mismatch?

  27. 27

    Why isn't overloading with a different generic type as a parameter allowed?

  28. 28

    Why is double not allowed as a non-type template parameter?

  29. 29

    Java - Why is it allowed to have Variable Name as Type Name

HotTag

Archive