Search | Synergex.com | Contact Us | Resource Center Login | Home|
News

What is the output of the following program?

namespace a
function fred, ^val
proc

    freturn 1    
end
endnamespace

namespace b
function fred, ^val
proc

    freturn 2
end
endnamespace

main
proc

    open (1,o,"TT:")
    writes(1,%string(%a.fred))     
end

a. "1"
b. "2"
c. this does not compile and link
d. this fails at runtime

ANSWER:

This program declares two functions named "fred" in different namespaces. It then calls the one declared in namespace "a," so we'd expect the output to be "1" (a) right?

Not so fast.  When functions and subroutines are imported from a namespace, they must be unique.  That's because you can look them up with %XADDR and %XSUBR, which don't support namespacing.  In this example, both namespaces "a" and "b" are implictly imported by virtue of being included in the same compilation with the mainline.  This results in a "Duplicate symbol" error in the linker or the .NET compiler.  Thus, (c) is the correct answer.

In order to resolve the ambiguity, we would need to make "fred" a static method of a class.  In that case, we can fully qualify the namespace.class.method name, and as long as any part of that path is unique the compiler can resolve it.

More information about News and Events

Contact Synergex to learn more about  News and Events