fbpx

the use of 'out' in a parameter list / situation

More
1 year 9 months ago #1 by Phil Hepburn
Hi guys,

Can anyone please suggest some X# syntax to show how to use the 'out' option in a parameter list.

I had a quick fumble around but with no good results.

End of day, brain hurts, nee my Forum colleagues ;-0)

Apparently Tuples can happily replace the use of 'OUT'.

HELP !!!

TIA,
Phil.

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #2 by Johan Nel
Replied by Johan Nel on topic the use of 'out' in a parameter list / situation
Phil,
Is that not REF?
METHOD SyMethod(in AS STRING, out REF STRING)
Johan

______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #3 by Phil Hepburn
Replied by Phil Hepburn on topic the use of 'out' in a parameter list / situation
Hi again,

Having had another end of day fumble, I think I have a start, you know, my foot in the door!

Here are a couple of small images :-





I got success once I stopped trying to include 'as'.

Have a nice weekend,
Phil.
Attachments:

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #4 by Phil Hepburn
Replied by Phil Hepburn on topic the use of 'out' in a parameter list / situation
Hi Johan,

I am not sure, I thought the REF idea was out of fashion a while back. These sorts of things are no much in my experience.

However, I do like the Tuple (sexy way) I will need to experiment.

I will feed back here if I get anything useful.

Regards,
Phil.

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #5 by Robert van der Hulst
Replied by Robert van der Hulst on topic the use of 'out' in a parameter list / situation
Johan,

See https://www.xsharp.info/help/parameters.html
parameterList      : LPAREN (parameter (COMMA parameter)*)? RPAREN
                   ;
 
parameter          : (attributes)? SELF? identifier (ASSIGN_OP expression)? (parameterDeclMods datatype)?
                      | ELLIPSIS
                   ;
 
parameterDeclMods   : (AS | REF | OUT | IS | PARAMS) CONST?
                   ;

So AS, REF, OUT, IS and PARAMS are mutually exclusive.

Robert

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #6 by Johan Nel
Replied by Johan Nel on topic the use of 'out' in a parameter list / situation
Thanks Robert,

Sorry for any confusion, responded and then only checked the manual and did see OUT is recognized.

The same effect can be "simulated" with REF, provided you initialize the parameter before passing, which the OUT does not require. Both have its place.

REF: The parameter might be changed by the called method.
OUT: The parameter has to be set by the called method.

Regards,

Johan

______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #7 by Phil Hepburn
Replied by Phil Hepburn on topic the use of 'out' in a parameter list / situation
An interesting short post Johan,

Thanks, I will now give these ideas a go in a test 'rig'.

Speak soon,
Phil.

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #8 by Frank Maraite
Replied by Frank Maraite on topic the use of 'out' in a parameter list / situation
Hi Johan,
this

Johan Nel wrote:
REF: The parameter might be changed by the called method.
OUT: The parameter has to be set by the called method.


wasn't clear to me. Thanks!

Does this mean OUT parameters are always NULL at the beginning and should be tested !NULL before return?

Frank

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #9 by Johan Nel
Replied by Johan Nel on topic the use of 'out' in a parameter list / situation
Hi Frank,

Frank Maraite wrote: this wasn't clear to me. Thanks!
Does this mean OUT parameters are always NULL at the beginning and should be tested !NULL before return?

No, it will raise an error during compilation that the parameter is not set.
FUNCTION TestOut(h AS STRING, w OUT STRING) AS STRING
RETURN h + " " + w
error XS0177: The out parameter 'w' must be assigned to before control leaves the current method

Regards Johan

______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #10 by Phil Hepburn
Replied by Phil Hepburn on topic the use of 'out' in a parameter list / situation
Hi guys - Johan, Frank, and any others interested,

Attached are a couple of small images to help show the functionality etc.

OUT must change in the method code, and REF can change but does not have to. That's how it seems to me.

I suggest you try these out carefully for yourself before you use them, any guy who is not familiar with their use. Just so you know what is going on - check below :-











Hope this interests a few of you ;-0)

Cheers,
Phil.

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #11 by Johan Nel
Replied by Johan Nel on topic the use of 'out' in a parameter list / situation
Hi Phil,

Think you have summed it up quite nicely.
Maybe look at a string example, where the sRef have to be initialized before the call. Remember numbers default to Zero.

Johan

______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #12 by Phil Hepburn
Replied by Phil Hepburn on topic the use of 'out' in a parameter list / situation
Hi Johan,

I actually tried the strings first, but thought I had too many images to post. I also thought that int and string would be different since we can have a nullable ints but that strings don't - and a default would be an empty string.

Please feel free to add your knowledgeable details to these captured images :-















Hope this interests a few guys,
Cheers,
Phil.

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #13 by Johan Nel
Replied by Johan Nel on topic the use of 'out' in a parameter list / situation
Hi Phil,

Think you can maybe just show the warning message when cRef is not initialized in the calling block before passed into the calling method/func/proc.

Johan

______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #14 by Phil Hepburn
Replied by Phil Hepburn on topic the use of 'out' in a parameter list / situation
Hi Johan,

An earlier message of yours suggested that I needed to do more than I seemed able at this end.

Is it that the X# version works differently to what you are referring to ? As regards Ref and Out variable inputs.

It seems possible for me to pass in Ref and Out, both of which have not been assigned a value, just declared as a value type. I get no errors for this - see below :-





When I tried it with INTs the results were as if the default of zero was applied.

What exactly do you wish me to do ?

Here are the inputs and results for DateTimes - it would appear that unassigned inputs to Ref and Out are given a default DateTime of 0001, 01, 01 00:00:00.

Check these small images :-







So as far as I can tell the compiler supplies default values if values don't exist.

Is this what you needed to know?

This behaviour could be a real "catch you out" (or 'gotcha' as Willie says) I feel - what do you think ?

Best regards,
Phil.

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #15 by Johan Nel
Replied by Johan Nel on topic the use of 'out' in a parameter list / situation
Hi Phil,
Sorry just tested now and seems the compiler by default set string to "" even if not specified /v02.
What I tried to highlight is that it might be dangerous if not initialized:
FUNCTION Start() AS VOID
  LOCAL o AS MyClass
  RefClassTest(o)
  ?o:RefValue
RETURN

FUNCTION RefClassTest(o REF MyClass) AS VOID
  o:RefValue := "Foo"
RETURN
CLASS MyClass
  EXPORTED RefValue AS STRING
  CONSTRUCTOR(v AS STRING)
    SELF:RefValue := v
  RETURN
END CLASS

You can try the following with a NULL string though and it will fail at runtime:
FUNCTION Start() AS VOID
  LOCAL s AS STRING
  TestRef(s)
  ?s
RETURN
FUNCTION TestRef(s REF STRING) AS VOID
  IF s:Length > 0
    s := "String length > 0"
  ELSE
    s := "String length = 0"
  ENDIF
RETURN

______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.