Generic methods type inference

iamjoosy

Let's say I have a class with two generic methods:

TMyClass = class
  procedure DoWith<T: class> (obj: T);
  procedure DoFor<T: class> ( proc: TProc<T> );
end;

Now, when I want to call either of these two methods with a specific type parameter, Delphi can infer the type for the DoWith method, so I can call it with either

MyClass.DoWith <TButton> ( MyButton )

or

MyClass.DoWith ( MyButton )

The Delphi Compiler will happily compile both. But if I omit the type parameter in the DoFor method, the Delphi compiler complains about the missing type parameter:

MyClass.DoFor<TButton>(procedure (Button: TButton) begin .... end);  // compiles


MyClass.DoFor(procedure (Button: TButton) begin .... end);  // doesn't compile

Now my question is: Is this just a shortcoming of the compiler, or is there any logical reason (that I haven't figured out yet) that prohibits the compiler from correctly inferring the type for the DoFor method?

Stefan Glienke

The reason it cannot infer T from a TProc<T>argument is that at that time TProc<TButton> is a constructed type without any information that it originally was a TProc<T>.

To do that it would have to infer the type from the anonymous method signature which does not work (I guess Barry Kelly could explain that better and I think he once wrote about the difficulties of lambdas and type inference in Delphi).

The only type inference the Delphi compiler is capable of is an argument of type T. Even with multiple arguments that does not work often and even less if you have more than one generic type parameters.

Edit: I found a comment where Barry explained a bit about the difficulties of type inference and lambdas in the Delphi compiler: http://www.deltics.co.nz/blog/posts/244/comment-page-1#comment-107

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does type inference work with overloaded generic methods

From Java

Strange type inference behavior of Collections.emptyList() and/or Java generic methods?

From Dev

FSharp: Type inference with generic

From Dev

Swift Generic Type Inference

From Dev

Regressive generic type inference

From Dev

TypeScript Generic Type Inference

From Dev

Typescript generic of generic type inference

From Java

strange issue with Generic type inference

From Java

Generic type inference limits in Java

From Dev

Why is the inference of a generic type not "transitive"?

From Dev

Swift: Generic type inference at runtime

From Dev

Generic factory method and type inference

From Dev

Generic call signatures and type inference

From Dev

Generic Type Inference with Class Argument

From Dev

Generic Type Inference in C#

From Dev

Swift Type Inference with Generic method

From Dev

Typescript: Generic class type inference

From Dev

Generic extension method type inference

From Dev

Type Inference for a mixed in generic trait

From Dev

Generics and type inference for a generic factory

From Dev

Type Inference for delegate parameters to methods

From Java

Java Generics: Question regarding type capture and generated inference using generic methods

From Dev

Type inference in generic method & generic class

From Java

Local type inference and contravariance in generic type variables

From Java

Java type inference of generic exception type

From Dev

Kotlin - get the non-nullable inference of a nullable generic type inference

From Dev

Typescript generic type inference based on inheritance

From Java

Generic type inference not working with method chaining?

From Java

Generic type parameters inference in method chaining

Related Related

  1. 1

    How does type inference work with overloaded generic methods

  2. 2

    Strange type inference behavior of Collections.emptyList() and/or Java generic methods?

  3. 3

    FSharp: Type inference with generic

  4. 4

    Swift Generic Type Inference

  5. 5

    Regressive generic type inference

  6. 6

    TypeScript Generic Type Inference

  7. 7

    Typescript generic of generic type inference

  8. 8

    strange issue with Generic type inference

  9. 9

    Generic type inference limits in Java

  10. 10

    Why is the inference of a generic type not "transitive"?

  11. 11

    Swift: Generic type inference at runtime

  12. 12

    Generic factory method and type inference

  13. 13

    Generic call signatures and type inference

  14. 14

    Generic Type Inference with Class Argument

  15. 15

    Generic Type Inference in C#

  16. 16

    Swift Type Inference with Generic method

  17. 17

    Typescript: Generic class type inference

  18. 18

    Generic extension method type inference

  19. 19

    Type Inference for a mixed in generic trait

  20. 20

    Generics and type inference for a generic factory

  21. 21

    Type Inference for delegate parameters to methods

  22. 22

    Java Generics: Question regarding type capture and generated inference using generic methods

  23. 23

    Type inference in generic method & generic class

  24. 24

    Local type inference and contravariance in generic type variables

  25. 25

    Java type inference of generic exception type

  26. 26

    Kotlin - get the non-nullable inference of a nullable generic type inference

  27. 27

    Typescript generic type inference based on inheritance

  28. 28

    Generic type inference not working with method chaining?

  29. 29

    Generic type parameters inference in method chaining

HotTag

Archive