Disambiguating pointers to overloaded functions

General FreeBASIC programming questions.
Post Reply
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Disambiguating pointers to overloaded functions

Post by shadow008 »

You can disambiguate pointers to overloaded functions like so:

Code: Select all

function test overload (i as integer ptr) as integer
	return 100
end function

function test overload (s as string ptr) as integer
	return 999
end function

dim x as integer
dim y as string

dim funcPtrx as function(as typeof(@x)) as integer = @test
dim funcPtry as function(as typeof(@y)) as integer = @test

print funcPtrx(@x)
print funcPtry(@y)
You can even punt this into a function pointer that has a generic signature:

Code: Select all

...
dim funcPtrA as function(as any ptr) as integer = cast(function(as any ptr) as integer, funcPtrx)
dim funcPtrB as function(as any ptr) as integer = cast(function(as any ptr) as integer, funcPtry)

print funcPtrA(0)
print funcPtrB(0)
But I can't figure out a way to do this directly with cast. This is what I wish to achieve:

Code: Select all

function test overload (i as const integer ptr) as integer
	return 100
end function

function test overload (s as string ptr) as integer
	return 999
end function

dim x as integer
dim y as string

dim funcPtrx as function(as typeof(@x)) as integer = @test
dim funcPtry as function(as typeof(@y)) as integer = @test

'Is there a way to directly assign the string variant of "test" using cast?
dim funcPtrA as function(as any ptr) as integer = cast(function(as typeof(@y)) as integer, @test)

print funcPtrx(@x)
print funcPtry(@y)

print funcPtrA(0) 'Prints the wrong output, should be 999
Edit:
In playing around with it I'm starting to think this is a bug. The compiler should be able to recognize that the cast is attempting to specify the function signature but isn't doing so.
shadow008
Posts: 86
Joined: Nov 26, 2013 2:43

Re: Disambiguating pointers to overloaded functions

Post by shadow008 »

After much looking the answer is using ProcPtr(): https://www.freebasic.net/wiki/KeyPgOpProcptr

This allows for some crazy compile time type shenanigans:

Code: Select all

function test overload (s as string ptr) as integer
	return 999
end function

function test overload (i as integer ptr) as integer
	return 100
end function

sub final(inFunc as function(as any ptr) as integer)
	print inFunc(0)
end sub

dim x as integer
dim y as string

print test(@x)
print test(@y)

final(cast(function(as any ptr) as integer, procptr(test, function(as typeof(@x)) as integer)))
final(cast(function(as any ptr) as integer, procptr(test, function(as typeof(@y)) as integer)))
Post Reply