optional parameter

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
baramuse
Posts: 98
Joined: Tue Nov 29, 2022 8:31 am
Location: France

optional parameter

Post by baramuse »

Hi,

I'm trying to understand when the compiler is complaining about a decalred optional parameter.
I have my function in a Xsharp project :

Code: Select all

METHOD LocCreation( cCodeClient AS STRING, cMotDePasse AS STRING, aArticleLoc AS List<ArticleLoc>, dtDebutLoc AS DATETIME, dtFinLoc AS DATETIME, dtDepart AS DATETIME, ;
				dtRetour AS DATETIME, nForfaitLoc AS REAL8, nForfaitPresta AS REAL8, nForfaitPort AS REAL8, lResa AS LOGIC, cNumLoc AS STRING, cLivr1 := "" AS STRING , ;
				cLivr2 := NULL AS STRING?, cCodePer1 := NULL AS STRING?, cCommentaire := NULL AS STRING?, cRefClt := NULL AS STRING?) AS LocStatut
called by an ASP.net project and compiler complains :

Code: Select all

Error (active)	CS7036	There is no argument given that corresponds to the required parameter 'cLivr1' of 
'LocasystWebServices.LocCreation(string, string, List<ArticleLoc>, DateTime, DateTime, DateTime, DateTime, double, double, double, bool, string, string, string?, string?, string?, string?)'	LocasystServices.asmx.cs	682	
Regards.
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: optional parameter

Post by robert »

Basile,

Default values for optional parameters are handled by the X# compiler in 2 different ways:
- In the Core dialect, it handles them just like C#. Default must be standard .Net constants.
- In the other dialects, the default values are stored as special attributes on the parameters. This allows for defaults for x# datatypes. The Asp.Net project (C# I assume) does not understand these attributes.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
baramuse
Posts: 98
Joined: Tue Nov 29, 2022 8:31 am
Location: France

Re: optional parameter

Post by baramuse »

Thank you Robert.

The aps.net project id indeed c#.

Does that means I would need to have my XSharp library as a Core dialect project in order to be able to use optional parameters ?
User avatar
Chris
Posts: 4897
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: optional parameter

Post by Chris »

Hi,

What's the exact code you are using to call the method?

Btw, using "STRING?" to denote a nullable string is not correct, the STRING type (System.String) is a reference type (like ArrayList, Form etc, or any class you define with the CLASS statement), so it is already possible to assign NULL for it and test it agains NULL. It's only structures (like INT, DateTime etc) that can be nullable, as those always represent a value in their regular form.

So the compiler should had reported an error on "STRING?", will open a ticket for it. I think that currently it just ignores the "?".
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
baramuse
Posts: 98
Joined: Tue Nov 29, 2022 8:31 am
Location: France

Re: optional parameter

Post by baramuse »

@Chris,

yes indeed, I was trying nullable before (like the other following parmeters)
And also no, I'm writing a lot of more modern c# and there is a difference between string and string? (starting c#8) that can come handy, when specifying APIs for example

https://learn.microsoft.com/en-us/answe ... tring-in-c
https://learn.microsoft.com/en-us/dotne ... ence-types

The calling is :

Code: Select all

this.Statut = oLWS.LocCreation(this.CodeClient, "_PgsWebOk_", ArticleLocListe, Debut, Fin, Depart, Retour, ForfaitLoc, ForfaitPresta, ForfaitPort, false, null);
User avatar
Chris
Posts: 4897
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: optional parameter

Post by Chris »

Hi Basile,

I was not aware of that, thanks! So the (roslyn) compiler can be setup so that it does not even allow assigning null to "regular" reference types! But when I try this in X#

Code: Select all

LOCAL c AS STRING?
c := NULL
the compiler reports "warning XS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context." and I don't see how we can define such a context in X#. So we probably need to make some adjustments in X# to fully support this. Unless Robert/Nikos, have you already done something about this?
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
baramuse
Posts: 98
Joined: Tue Nov 29, 2022 8:31 am
Location: France

Re: optional parameter

Post by baramuse »

baramuse wrote: Mon Nov 18, 2024 11:00 am Thank you Robert.

The aps.net project id indeed c#.

Does that means I would need to have my XSharp library as a Core dialect project in order to be able to use optional parameters ?
well, I don't think I can convert the Xsharp lib to Core dialect so I'll just create overloads.

But now I understand why at least :)
User avatar
baramuse
Posts: 98
Joined: Tue Nov 29, 2022 8:31 am
Location: France

Re: optional parameter

Post by baramuse »

Chris wrote: Mon Nov 18, 2024 11:42 am Hi Basile,

I was not aware of that, thanks! So the (roslyn) compiler can be setup so that it does not even allow assigning null to "regular" reference types! But when I try this in X#

Code: Select all

LOCAL c AS STRING?
c := NULL
the compiler reports "warning XS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context." and I don't see how we can define such a context in X#. So we probably need to make some adjustments in X# to fully support this. Unless Robert/Nikos, have you already done something about this?
Well to be honest my project configuration is a bit more exotic :
I have an existing legacy asp.net project service an API in SOAP and calling most of the methods from a Vulcan.net dialect library.

Then in order to serve a more morden way I've created a .net-8 core api project, referencing an sdk-style project holding references to the .prg sources of the vulcan.net xsharp lib.
that project does have the "nullable enable" to true :
<PropertyGroup>
<OutputType>library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>PGS.WebServices.Lib.XS_core</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<TargetPlatFormIdentifier>Windows</TargetPlatFormIdentifier>
<Nullable>enable</Nullable>
<Dialect>Vulcan</Dialect>
<INS>True</INS>
<InitLocals>True</InitLocals>
<LB>False</LB>
<Unsafe>False</Unsafe>
<OVF>True</OVF>
<EnforceSelf>True</EnforceSelf>
<Allowdot>false</Allowdot>
<StandardDefs>
</StandardDefs>
<Vo3>False</Vo3>
<Vo2>True</Vo2>
<Vo4>True</Vo4>
<Vo11>True</Vo11>
<Vo12>True</Vo12>
<Vo13>True</Vo13>
<Vo7>True</Vo7>
<Vo8>True</Vo8>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Locasyst.WebServices.Lib.XSharp\Calcul Dispo.prg" />
(...)
</ItemGroup>
So I build my .net-core Xsharp lib with dotnet build , then I reference the lib in my .net-8 core api project and I have the same source code for the library used by the legacy asp.net project and .net8 core api project
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: optional parameter

Post by robert »

Chris,
We have not yet enabled support for #nullable context in X#.
We'll probably do that in X# 3.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply