title: SML/NJ fail to override a function in other modules
body: <p>I want to override <code>print</code> to let it output to outstreams other than <code>stdout</code>. I have two files: <code>m1.sml</code> and <code>main.sml</code> in the same directory. I want to override the behavior of <code>print</code> in both files but it fails to override the <code>print</code> inside <code>m1.sml</code>.</p>
  
  <p><strong>m1.sml</strong></p>
  
  <pre><code>structure Demo : sig
    val printSomething : string -&gt; unit
    end = 
    struct
      fun printSomething s = print s
    end
  </code></pre>
  
  <p><strong>main.sml</strong></p>
  
  <pre><code>use "m1.sml";
  
  fun redirectPrint strm str = TextIO.output (strm, str);
  
  fun testing output = 
    let 
        val outStrm = TextIO.openOut output
        val print = redirectPrint outStrm 
    in
      (print "This will show up in test.txt\n"; 
       Demo.printSomething "This won't show up in test.txt\n"; 
       TextIO.closeOut outStrm)
    end;
  
  testing "test.txt"
  </code></pre>
  
  <p>Running <code>sml main.sml</code> will yield </p>
  
  <pre><code>...
  [autoloading done]
  val redirectPrint = fn : TextIO.outstream -&gt; TextIO.vector -&gt; unit
  val testing = fn : string -&gt; unit
  this won't show up in test.txt
  val it = () : unit
  </code></pre>
  
  <hr>
  
  <p>As you have seen in the output, it works for the line </p>
  
  <pre><code>print "This will show up in test.txt\n" (written to test.txt)
  </code></pre>
  
  <p>but not the next line </p>
  
  <pre><code>Demo.printSomething "This won't show up in test.txt\n"; (printed to stdout)
  </code></pre>
  
  <p>which just prints to <code>stdout</code>. </p>
  
  <p><code>Demo.printSomething</code>, however, also calls <code>print</code>. Also, in the current scope where <code>Demo.printSomething</code> is called, <code>print</code> was overridden. </p>
  
  <p>I wonder why this happens?</p>