Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

AScan generic and codeblock types 02 Dec 2022 11:59 #24614

  • bmellac
  • bmellac's Avatar
  • Topic Author


  • Posts: 29
  • 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.
    Erreur	XS1503	Argument 1: cannot convert from 'array of<ArtDet>' to 'array'

    The culprit is there :
    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
    AScan<ArtDet>( aArtDetList, { |oItem AS ArtDet| oItem:CodeArt == oArtLivr:CODEART .AND. oItem:EtatOccaz == oArtLivr:ETATOCCAZ } )
    But then I've got
    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 ?
    AScan<ArtDet>( aArtDetList, { |oItem| oItem:CodeArt == SELF:oFacDet:CODEART .AND. oItem:EtatOccaz == SELF:oFacDet:ETATOCCAZ } )
    ... nope :
    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

    Please Log in or Create an account to join the conversation.

    AScan generic and codeblock types 02 Dec 2022 12:20 #24615

    • robert
    • robert's Avatar


  • Posts: 3588
  • 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():
    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
    FUNCTION AScan<T>(aTarget AS ARRAY OF<T>,uSearch AS T) AS DWORD

    The lambda solution would look like this (unchecked):
    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

    Please Log in or Create an account to join the conversation.

    AScan generic and codeblock types 02 Dec 2022 12:54 #24618

    • bmellac
    • bmellac's Avatar
    • Topic Author


  • Posts: 29
  • 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 ?

    Please Log in or Create an account to join the conversation.

    AScan generic and codeblock types 02 Dec 2022 13:23 #24620

    • robert
    • robert's Avatar


  • Posts: 3588
  • Basile,

    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

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1