Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

  1. Add public to kit's import of scores and make noleak.tiko compile.
  2. Import greet directly in reexport.tiko as well, and call it both ways.