[SOLVED] namespace does not call properly overloaded function

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

[SOLVED] namespace does not call properly overloaded function

Post by shadow008 »

Consider the following code:

Code: Select all

function hash overload (inval as string) as integer
    return 999
end function

namespace ns
    function hash overload (inval as integer) as integer
        return hash("hello") 'Type mismatch at parameter 1 of HASH
    end function
    
    print hash("string") 'Illegal inside a NAMESPACE block?
    print hash(10) 'Illegal inside a NAMESPACE block?
    
end namespace
There appear to be 2 similar, but not identical bugs related to this behavior:
https://sourceforge.net/p/fbc/bugs/902/ Overloaded operators do not respect namespace
https://sourceforge.net/p/fbc/bugs/972/ A local Type cannot access members of an inherited Type external to its scope but normally accessible[Resolved] (Note this pertains to accessing a type member, not a globally defined symbol so not entirely related)

Two questions come up:
1) The namespace should be able to access the globally defined version of hash in this code, no? Is this distinct from bug 902?
2) What is illegal about calling those functions within the namespace?

I'm not seeing anything related to 2 in the documentation, and I think 1 is just a straight up bug. Can someone clarify what's going on here?
Last edited by shadow008 on Jul 06, 2023 20:04, edited 1 time in total.
adeyblue
Posts: 301
Joined: Nov 07, 2019 20:08

Re: [BUG] namespace does not call properly overloaded function

Post by adeyblue »

You can call it if you put a dot before it

Code: Select all

function hash overload (inval as string) as integer
    return 999
end function

namespace ns
    function hash overload (inval as integer) as integer
        return .hash("hello") 'works
    end function
    
end namespace
It's still probably a bug (I don't know how 'the man' wants the language to work but I expect the original code should work) but this shows that you can call it by explicitly using the namespace.
2) What is illegal about calling those functions within the namespace?
You can only put code 'anywhere' at the top level (ie outside of any UDT's and namespaces), otherwise it needs to be in a sub or function.

Code: Select all

function hash overload (inval as string) as integer
    return 999
end function

namespace ns
    function hash overload (inval as integer) as integer
        return .hash("hello") 'works
    end function
    
    '' inside a namespace, this is no good
    ''print hash("string") 'Illegal inside a NAMESPACE block?
    ''print hash(10) 'Illegal inside a NAMESPACE block?
    
    '' inside a sub, this is fine
    sub fun()
        print .hash("string")
        print hash(10)
    end sub
    
end namespace

'' outside of any namespace or udt, is fine
print hash("string")
print ns.hash(10)
ns.fun()
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [BUG] namespace does not call properly overloaded function

Post by fxm »

- First remark : A namespace bloc can only contain declarations and procedures definitions.
- Second remark : 'Overload' qualifier is useless because the two procedures are not in the same scope (global for the first and local for the second).
- Third remark : To access from a local scope a duplicated name in the global scope, use '..' as prefix.

Code: Select all

function hash (inval as string) as integer
    return 999
end function

namespace ns
    function hash (inval as integer) as integer
        return ..hash("hello") 'Type mismatch at parameter 1 of HASH
    end function
    
'    print hash("string") 'Illegal inside a NAMESPACE block?
'    print hash(10) 'Illegal inside a NAMESPACE block?
    
end namespace
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [BUG] namespace does not call properly overloaded function

Post by fxm »

Note:
- Presently '.' or '..' allows access to a duplicate name in the global scope, except from a 'With...End With' block where only '..' is valid (because '.' is already used as shortcut to access members).
- It is therefore recommended to use '..' everywhere to avoid confusion.
- In future releases, maybe only '..' will be authorized for this feature.
Post Reply