xsharp.eu • Discard variable for OUT parameters
Page 1 of 1

Discard variable for OUT parameters

Posted: Tue Nov 19, 2019 9:11 am
by leon-ts
C# 7 introduced a very convenient thing (quotation from whatsnew):
Often when deconstructing a tuple or calling a method with out parameters, you're forced to define a variable whose value you don't care about and don't intend to use. C# adds support for discards to handle this scenario. A discard is a write-only variable whose name is _ (the underscore character); you can assign all of the values that you intend to discard to the single variable. A discard is like an unassigned variable; apart from the assignment statement, the discard can't be used in code.
Is there something similar in XSharp?

In some cases, this is convenient when overloading methods/functions.

Code: Select all

function MyFunc(x as int, additionalResult out int) as logic
    ...
    return true

function MyFunc(x as int) as logic
    local additionalResult as int
    return MyFunc(x, out additionalResult)
Compiling this code generates a warning: XS0219 The variable 'additionalResult ' is assigned but its value is never used

Best regards,
Leonid

Discard variable for OUT parameters

Posted: Tue Nov 19, 2019 9:59 am
by robert
Leonid,

We do not support this (yet) but I will put it on the list.
I had a quick look at the C# implementation and it is certainly doable.
The only problem that I see is that some people may have already used the single underscore as variable name.

Robert

Discard variable for OUT parameters

Posted: Tue Nov 19, 2019 10:08 am
by leon-ts
Robert,
but I will put it on the list
Glad to hear that!

The only problem that I see is that some people may have already used the single underscore as variable name.
The name of the keyword is not the main thing. It is important that you have the ability to discard variables for OUT parameters as C#.

Best regards,
Leonid

Discard variable for OUT parameters

Posted: Tue Nov 19, 2019 11:41 am
by leon-ts
Robert,

I can offer options for name of the keyword:
OUT NIL
OUT NULL

Best regards,
Leonid

Discard variable for OUT parameters

Posted: Tue Nov 19, 2019 5:13 pm
by Chris
OUT NULL was what first came to my mind as well!