SUB pointer and optional single ANY PTR parameter

General FreeBASIC programming questions.
Post Reply
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

SUB pointer and optional single ANY PTR parameter

Post by fxm »

Since fbc 0.90.0, calling through a procedure pointer does not fully support an optional "Any Ptr" parameter:

Code: Select all

Type t As Any Ptr

Sub test (Byval p As t = 0)
End Sub

test(0)
test()

Dim As Sub (Byval As t = 0) ptest = @test
ptest(0)
ptest()
Compiler output:
...\FBIDETEMP.bas(11) error 1: Argument count mismatch, found ')' in 'ptest()'

That compiles with any other pointer type.
Last edited by fxm on Nov 04, 2018 22:46, edited 1 time in total.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Procedure pointer and optional ANY PTR parameter

Post by Munair »

Thanks.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Procedure pointer and optional ANY PTR parameter

Post by fxm »

But for me, I consider this a bug unless someone gives me the reason for why.

I wait a little your reactions before filling a bug report.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Procedure pointer and optional ANY PTR parameter

Post by D.J.Peters »

@fxm it's a tiny bug
but a bug is a bug :-)

The lexer has a problem if the only param are of type "any" and has a default value of "NULL"

Joshy

Code: Select all

Type TANY As any ptr

Sub test (byval first as integer=0, Byval second As TANY=0)
End Sub

test():test(1):test(,0):test(1,0)

Dim As Sub (byval first as integer=0, Byval second As TANY=0) ptest = @test
ptest() : ptest(1) : ptest(,0) : ptest(1,0)

sleep
Last edited by D.J.Peters on Nov 04, 2018 21:49, edited 1 time in total.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Procedure pointer and optional ANY PTR parameter

Post by Munair »

No, this looks like a bug. I meant thanks for pointing it out.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Procedure pointer and optional ANY PTR parameter

Post by MrSwiss »

The bug seems to be in line 1 (type t, instead of Dim As ...)

Code: Select all

Dim As Any Ptr t

Sub test (Byval p As Any Ptr = 0)
End Sub

test(0)
test()

Dim As Sub (Byval As Any Ptr = 0) ptest = @test
ptest(0)    '<---- works correct to here
'ptest()    ' argument count mismatch (error 1)
Sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Procedure pointer and optional ANY PTR parameter

Post by dodicat »

It woks for function OK
The closest I can get to sub is:

Code: Select all

 

function test (Byval p As Any Ptr = 0) as sub
    print "test"
    return p
End function

test(0)
test()

Dim As function (Byval As Any Ptr = 0) as sub ptest = @test
ptest(0)   
ptest() 


#print typeof(test)


Sleep 
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Procedure pointer and optional ANY PTR parameter

Post by St_W »

MrSwiss wrote:The bug seems to be in line 1 (type t, instead of Dim As ...)
Those are different things. What fxm is using is called a type alias (see https://freebasic.net/wiki/wikka.php?wa ... ypeAliases ; you may also know "typedef" from C).

@dodicat: didn't know that such syntax is even legal/possible in FB :-)
Congrats for finding that workaround. However, I wouldn't use such code in a "real" application.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SUB pointer and optional single ANY PTR parameter

Post by fxm »

OK dodicat.

Code: Select all

Sub test (Byval p As Any Ptr = 0)
  Print "test"
End Sub

test(0)
test()

Dim As Function (Byval As Any Ptr = 0) As Integer ptest = Cast(Any Ptr, @test)
ptest(0)
ptest()
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SUB pointer and optional single ANY PTR parameter

Post by fxm »

Thank you all for clarifying the conditions for obtaining this compilation error that I consider a compilation bug.

Bug report filled in:
#892 Bug when default calling via a pointer to a 'Sub' defined with the only 'Any Ptr' optional parameter
Post Reply