Menu

#843 The implicit casting of argument to string works for Instr() and Mid() but not for Left() and Right()

closed
nobody
None
compiler
2021-08-02
2016-11-12
No

For an UDT with a member operator Cast to String, an instance passed as argument is well implicitly converted to string when calling Instr() and Mid(), but not when calling Left() and Right():

Type UDT
  Declare Operator Cast() As String
  Dim As Integer I
End Type
Operator UDT.cast() As String
  Return Str(This.I)
End Operator

Dim As UDT u
u.I = 123

Print Mid(u, 2)
Print Instr(u, "2")
Print Left(u, 2)
Print Right(u, 2)

error 97: Ambiguous call to overloaded function, LEFT() in 'Print Left(u, 2)'
error 97: Ambiguous call to overloaded function, RIGHT() in 'Print Right(u, 2)'

Related

Bugs: #666

Discussion

  • fxm (freebasic.net)

    Same behavior for ZString and WString.

    Example for WString:

    Type UDT
      Declare Operator Cast() Byref As Wstring
      Dim As Integer I
    End Type
    Operator UDT.cast() Byref As Wstring
      Static As Wstring * 21 w
      w = Wstr(This.I)
      Return w
    End Operator
    
    Dim As UDT u
    u.I = 123
    
    Print Mid(u, 2)
    Print Instr(u, "2")
    Print Left(u, 2)
    Print Right(u, 2)
    

    error 97: Ambiguous call to overloaded function, LEFT() in 'Print Left(u, 2)'
    error 97: Ambiguous call to overloaded function, RIGHT() in 'Print Right(u, 2)'

     

    Last edit: fxm (freebasic.net) 2016-12-10
  • fxm (freebasic.net)

    That also does not work with Val().

     
  • Jeff Marshall

    Jeff Marshall - 2021-08-02
    • status: open --> closed
     

Log in to post a comment.