Quantcast
Channel: Delphi – The Wiert Corner – irregular stream of stuff
Viewing all articles
Browse latest Browse all 1440

Yes Delphi does some type inference on generic method calls

$
0
0

Delphi does indeed so some type inference as Primoz found out below. It has been doing this for a long time, improved over time, but has a long road ahead.

One odd thing is that generics (and the majority of inference support) were introduced in Delphi 2009 ([WayBack] Generics with Delphi 2009 Win32), but the inference was already documented in Delphi 2007: [WayBack] Delphi 2007 – Declaring Generics: Parameterized Types

This is a reminder to myself to write some more example code on what kinds of inference work and which do not, especially because of the comments from David Heffernan, Marco Cantu and Stefan Glienke, especially since the improvement over time has been small. I am curious to see how the promised “working on it” by now lives up to reality.

David Heffernan
This is a rare piece of code where type inference actually works. Mostly it does not which is very frustrating.

It’s a little ironic that you ask why you don’t need to include <T>. Normally people ask about the much more frequent instances where you do need to include <T> because the compiler’s type inference is so weak.

David Heffernan
+Marco Cantù Much more commonly there are times when you want the compiler to infer the type, but it won’t. It would really make a difference to us if the compiler was better at this.

Marco Cantù
+David Heffernan I tend to agree the compiler should be much better at type inference. Working on it!

Stefan Glienke
Infering the generic argument from a constructed generic type would be great.

GuessTheType<T>(const x: TArray<T>);

var
  a: TArray<Integer>;
begin
  GuessTheType(a);

does not work although the compiler could infer the parameter for GuessTheType from its x parameter but currently it does not know that a originally was a TArray<T> (yes, I know array of T as signature works but that is a different thing).

P.S. +Marco Cantù btw how hard can it be to finally implement generic standalone routines without that ugly static type? Probably one of the highest voted feature requests: https://quality.embarcadero.com/browse/RSP-13724)

Source: [WayBack] Anybody here knows since when we don’t have to write when calling generic ProcSomething(param: T)? It was brought to my attention today that the… – Primož Gabrijelčič – Google+

–jeroen


Viewing all articles
Browse latest Browse all 1440

Trending Articles