AScan generic and codeblock types

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

AScan generic and codeblock types

Post by baramuse »

Hi all,

I've got a quick syntaxic question about how to use generic functions.
In our project we now have a compilation error (Xsharp 2.13) that wasn't happenning in 2.12.

Code: Select all

Erreur	XS1503	Argument 1: cannot convert from 'array of<ArtDet>' to 'array'
The culprit is there :

Code: Select all

LOCAL aArtDetList		AS ARRAY OF ArtDet
AScan( aArtDetList, { |oItem AS ArtDet| oItem:CodeArt == oArtLivr:CODEART .AND. oItem:EtatOccaz == oArtLivr:ETATOCCAZ } )
So aArtDetList is a typed list and it bothers AScan.
There is a generic version of AScan so I tried

Code: Select all

AScan<ArtDet>( aArtDetList, { |oItem AS ArtDet| oItem:CodeArt == oArtLivr:CODEART .AND. oItem:EtatOccaz == oArtLivr:ETATOCCAZ } )
But then I've got

Code: Select all

Erreur	XS1503	Argument 2: cannot convert from 'codeblock' to 'ArtDet'
FIne ! then I don't need to type oItem as it should infer from the generic type right ?

Code: Select all

AScan<ArtDet>( aArtDetList, { |oItem| oItem:CodeArt == SELF:oFacDet:CODEART .AND. oItem:EtatOccaz == SELF:oFacDet:ETATOCCAZ } )
... nope :

Code: Select all

Erreur	XS1061	'usual' does not contain a definition for 'CodeArt' and no accessible extension method 'CodeArt' accepting a first argument of type 'usual' could be found
Should I translate the codeblock as a lambda function (I'd rather not)

Regards
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

AScan generic and codeblock types

Post by robert »

Basile,

Yes, the best solution is to create a lambda or a function

If you have an ARRAY OF <Something> then there are the following options for AScan():

Code: Select all

FUNCTION AScan<T>(aTarget AS ARRAY OF<T>,act AS Func<T, LOGIC>)  AS DWORD
FUNCTION AScan<T>(aTarget AS ARRAY OF<T>,act AS Func<T, LOGIC>,nStart AS LONG) AS DWORD
FUNCTION AScan<T>(aTarget AS ARRAY OF<T>,act AS Func<T, LOGIC>,nStart AS LONG, nCount AS LONG) AS DWORD
The Func<T> is either a lambda expression or the name of a function that accepts an object of type T and returns a logic
Instead of the Func<T> you can also pass an object that you want to look for

Code: Select all

FUNCTION AScan<T>(aTarget AS ARRAY OF<T>,uSearch AS T) AS DWORD
The lambda solution would look like this (unchecked):

Code: Select all

AScan( aArtDetList, { oItem => oItem:CodeArt == oArtLivr:CODEART .AND. oItem:EtatOccaz == oArtLivr:ETATOCCAZ })
The function approach would only work if oArtLivr is also visible to the function.

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

AScan generic and codeblock types

Post by baramuse »

Thanks Robert !

Just to be sure to understand : codeblock is not possible because it can only return USUAL by design so it won't be able to match the AScan<T> pickedup by the compiler because aArtDetList is of type Array<ArtDet> right ?
If aArtDetList was of type Array than the USUAL overload would have been chosen and the codeblock would have been valid ?
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

AScan generic and codeblock types

Post by robert »

Basile,
bmellac post=24618 userid=7110 wrote:Thanks Robert !

Just to be sure to understand : codeblock is not possible because it can only return USUAL by design so it won't be able to match the AScan<T> pickedup by the compiler because aArtDetList is of type Array<ArtDet> right ?
If aArtDetList was of type Array than the USUAL overload would have been chosen and the codeblock would have been valid ?
That is correct.
Robert
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply