Re-exporting
An import belongs to the file that writes it. A module that uses scores
internally does not hand scores to its own importers, so it is free to
replace it later. When a module does want to pass one of its imports on, it
adds public to the #import:
#module "kit"
#import "../greet" public '' re-exported: importers reach kit.greet
#import "../scores" '' kit's own business
public function Report( byval who as ustring, _
byval points as int32 ) as ustring
return greet.Hello( who ) & " Your grade: " & scores.Grade( points )
end function
'' One import brings in kit, and with it the module kit passes on.
#import "./kit"
print kit.Report( "grace", 88 )
print kit.greet.Hello( "again" )
Output
Hello, Grace! Your grade: B
Hello, Again!
The program imports only kit. Because kit re-exports greet, the program
reaches it as kit.greet.Hello. kit imports scores without public, so
that one stays out of reach:
#import "./kit"
print kit.scores.Grade( 88 ) '' refused: kit did not re-export scores
noleak.tiko(3,11): error TKO0745: the namespace 'kit' has no member named 'scores'
|
3 | print kit.scores.Grade( 88 ) '' refused: kit did not re-export scores
| ^
A program that needs scores imports it itself. Re-export a module when your
own interface is incomplete without it: when a type from that module appears
in your public procedures, for example.
Activities
- Add
publictokit's import ofscoresand makenoleak.tikocompile. - Import
greetdirectly inreexport.tikoas well, and call it both ways.