3

What's the best way to get around the shortcoming of generic type inferencing of reference arguments, so that I don't have to specify the type in every call?

Update: I don't mind other (including non-generic) solutions as long as it works with multiple (any?) types.

This seems to still not have been resolved, although known for quite some time.

Please vote for a resolution to this on Embarcaderos Quality Central: Issue #78103.

From a comment by Barry Kelly to Generic Methods and Type Inferencing:

PS: Your example, in Tiburon, almost works. Method type inference works well for arguments passed by value. It doesn’t work for arguments passed by reference, unfortunately (the compiler is being too strict).

Now, almost three years later, I'm trying the same thing in Delphi XE, and it complains that:

[DCC Error] INIv1_Parser.pas(81): E2033 Types of actual and formal var parameters must be identical

When calling:

function FindDataItemValue<T>(ItemType: TDataItemType; out Value: T): Boolean;

With:

var MaxG: Real;
...
if Data.FindDataItemValue(PAR_MaxG, MaxG) and (MaxG = 2.5) then ...

Commonly suggested work-around: However, if I add the generalization on the call, it works fine; although annoying that it is even needed.

Update:

Thus far, The best I've come up with is to use either Variants or the TValue record from the Rtti module. Using variants I implement interfaces when I need to use objects, and store a reference to that (interface) in the variant.

1
  • I don't feel I've got a single suggestion about how to really get around the issue. The only suggestions so far is to add the type to the call, which is what I would like to get rid of. I'll edit my original question to better reflect this. Commented Apr 14, 2011 at 7:22

3 Answers 3

3

Type inference does not currently work for var and out parameters. I agree that it is highly annoying.

Sign up to request clarification or add additional context in comments.

1 Comment

The answer then, becomes: there is no work-around. Or, the work-around is to avoid generics altogether.
1

There is no workaround. You have to specify the type.

var
  MaxG: Real;
...
  if Data.FindDataItemValue<Real>(PAR_MaxG, MaxG) and (MaxG = 2.5) then ...

If you want Embarcadero to resolve this, then vote for this QC entry that is about your issue.

The QC entries with the highest votes get more attention.

1 Comment

Thanks for the link to QC issue #78103. I've voted for it. Thanks to Thorsten Engler for reporting it.
0

The best way is to do exactly what's demonstrated in the article you cite: Include the type parameters in the method call:

if Data.FindDataItemValue<Real>(PAR_MaxG, MaxG) ...

1 Comment

Well, that makes it work. However, I would like to get rid of the <PotentiallyLongAndComplicatedTypeName>-syntax; even if I have to sacrifice the use of generics altogheter, which are rather buggy when it starts to get complex any way (or I'm not using generics correctly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.