Page 1 of 1
optional parameter
Posted: Mon Nov 18, 2024 10:39 am
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.
Re: optional parameter
Posted: Mon Nov 18, 2024 10:57 am
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
Re: optional parameter
Posted: Mon Nov 18, 2024 11:00 am
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 ?
Re: optional parameter
Posted: Mon Nov 18, 2024 11:02 am
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 "?".
Re: optional parameter
Posted: Mon Nov 18, 2024 11:16 am
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);
Re: optional parameter
Posted: Mon Nov 18, 2024 11:42 am
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#
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?
Re: optional parameter
Posted: Mon Nov 18, 2024 11:44 am
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
Re: optional parameter
Posted: Mon Nov 18, 2024 11:52 am
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#
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
Re: optional parameter
Posted: Mon Nov 18, 2024 12:25 pm
by robert
Chris,
We have not yet enabled support for #nullable context in X#.
We'll probably do that in X# 3.
Robert