Recent

Author Topic: No default value for parameter of type Variant  (Read 7181 times)

bmy92

  • New Member
  • *
  • Posts: 11
No default value for parameter of type Variant
« on: March 04, 2016, 08:11:30 am »
Hi all,
I have changed from Lazarus 1.2.6 to 1.6.0.
For COM Automation purposes, I have a function that takes a parameter of type Variant, and this parameter is declared with a default value.

It was accepted and it worked in Lazarus 1.2.6. It is now rejected at compilation in Lazarus 1.6.0 :
Error : It is not possible to define a default value for a parameter of type "Variant".
I could not find description of this incompatibility in the User changes of FreePascal, nor in Lazarus release notes.
Any reason for this change ?


Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1070
Re: No default value for parameter of type Variant
« Reply #2 on: March 04, 2016, 11:01:33 am »
It's more likely due to http://wiki.freepascal.org/User_Changes_3.0#Default_values_are_now_properly_typechecked

Assignments to the variant type are handled via overloaded operators in the RTL units. Since operations on variants are not part of the language itself, the compiler has no knowledge whatsoever about the internal layout of a variant or how to assign values to it. I guess this may have worked by coincidence on certain platforms in the past in case the memory layout of a variant happened to map the data you assigned to the starting address, but that's guaranteed to break on some platforms and completely implementation-dependent.

bmy92

  • New Member
  • *
  • Posts: 11
Re: No default value for parameter of type Variant
« Reply #3 on: March 04, 2016, 11:24:01 am »
-> Zoran : This is not my case, because the default value is declared at interface. And the implementation repeats exactly the interface.

-> Jonas Maebe
The default value is Nil, that should be compatible with a COM object. There is also a second error:
- Incompatible types: got "Pointer" expected "Variant"
 
Taking account of your message, I changed the declaration to NOT have a default value. Same declaration at implementation.
Code: Pascal  [Select][+][-]
  1. function  runScript(const scriptName: WideString; const argsList: array of Variant;
  2.           const language: WideString = 'Basic'; const location: WideString = 'user';
  3.           const doc: Variant): Variant;
It still throws one error at the interface declaration:
- Default parameter required for "DOC"

This is bewildering because some lines before,  this function is accepted by the compiler:
Code: Pascal  [Select][+][-]
  1. function  MakePropertyValue(const PropName: WideString; const PropValue: Variant): Variant;

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1070
Re: No default value for parameter of type Variant
« Reply #4 on: March 04, 2016, 11:28:56 am »
I have to admit I'm not familiar at all with COM variants. Are they implicit pointer types, so that this would result in the compiler passing nil as parameter value (rather than assigning nil to such a COM variant and then passing the address of this variant-containing-a-nil-value? And do you know whether Delphi accepts this?

The error you get is because if you have a parameter with a default value, all parameters coming after that one also must have a default value. Otherwise the compiler cannot determine which parameters you are trying to pass explicitly and which ones should get a default value.

bmy92

  • New Member
  • *
  • Posts: 11
Re: No default value for parameter of type Variant
« Reply #5 on: March 04, 2016, 11:44:33 am »
The Free Pascal reference guide says:
Interfaces and COM or CORBA objects can be assigned to a
variant (basically because they are simply a pointer).
Quote
do you know whether Delphi accepts this?
I don't know, I quit using Delphi years ago. At that time I did not have a parameter Variant with default.

Quote
if you have a parameter with a default value, all parameters coming after that one also must have a default value.
Of course ! Shame on me.
But this is not indicated in the Free Pascal reference guide.  ;D

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1070
Re: No default value for parameter of type Variant
« Reply #6 on: March 04, 2016, 11:49:43 am »
Your citation from the reference manual is about COM and Corba interfaces, not variants. I'm quite sure a variable/parameter declared as plain "variant" is always a regular record (since there is no way to differentiate some special COM variant from a regular variant if you declare it as "variant"). There's also the "olevariant" type, but I have no idea at all about what it's for or how it works.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12156
  • FPC developer.
Re: No default value for parameter of type Variant
« Reply #7 on: March 04, 2016, 12:58:59 pm »
Delphi says "parameters of this type cannot have default values".

 

TinyPortal © 2005-2018