Discard variable for OUT parameters

This forum is meant for questions and discussions about the X# language and tools
Post Reply
leon-ts
Posts: 430
Joined: Fri Feb 03, 2017 1:43 pm

Discard variable for OUT parameters

Post 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
Best regards,
Leonid
User avatar
robert
Posts: 4292
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Discard variable for OUT parameters

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
leon-ts
Posts: 430
Joined: Fri Feb 03, 2017 1:43 pm

Discard variable for OUT parameters

Post 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
Best regards,
Leonid
leon-ts
Posts: 430
Joined: Fri Feb 03, 2017 1:43 pm

Discard variable for OUT parameters

Post by leon-ts »

Robert,

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

Best regards,
Leonid
Best regards,
Leonid
User avatar
Chris
Posts: 4599
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Discard variable for OUT parameters

Post by Chris »

OUT NULL was what first came to my mind as well!
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
Post Reply