Next: , Up: TC-A   [Contents][Index]


4.12.1 TC-A Samples

Overloaded functions are not supported in regular Tiger.

let
  function null(i: int) : int    = i = 0
  function null(s: string) : int = s = ""
in
  null("123") = null(123)
end

File 4.45: sizes.tig

$ tc -Xb sizes.tig
error→sizes.tig:3.3-41: redefinition: null
error→sizes.tig:2.3-40: first definition
⇒4

Example 4.58: tc -Xb sizes.tig

Instead of regular binding, overloaded binding binds each function call to the set of active function definitions. Unfortunately displaying this set is not implemented, so we cannot see them in the following example:

$ tc -X --overfun-bindings-compute -BA sizes.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x55940eca7890 */() =
  (
    let
      function null /* 0x55940eca7c20 */(i /* 0x55940ecaa700 */ : int /* 0 */) : int /* 0 */ =
        (i /* 0x55940ecaa700 */ = 0)
      function null /* 0x55940eca8610 */(s /* 0x55940eca9ce0 */ : string /* 0 */) : int /* 0 */ =
        (s /* 0x55940eca9ce0 */ = "")
    in
      (null /* 0 */("123") = null /* 0 */(123))
    end;
    ()
  )

Example 4.59: tc -X --overfun-bindings-compute -BA sizes.tig

The selection of the right binding cannot be done before type-checking, since precisely overloading relies on types to distinguish the actual function called. Therefore it is the type checker that “finishes” the binding.

$ tc -XOBA sizes.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x55ce2d384890 */() =
  (
    let
      function null /* 0x55ce2d386ed0 */(i /* 0x55ce2d385e00 */ : int /* 0 */) : int /* 0 */ =
        (i /* 0x55ce2d385e00 */ = 0)
      function null /* 0x55ce2d384b00 */(s /* 0x55ce2d3850a0 */ : string /* 0 */) : int /* 0 */ =
        (s /* 0x55ce2d3850a0 */ = "")
    in
      (null /* 0x55ce2d384b00 */("123") = null /* 0x55ce2d386ed0 */(123))
    end;
    ()
  )

Example 4.60: tc -XOBA sizes.tig


There can be ambiguous (overloaded) calls.

let
  type foo = {}
  function empty(f: foo) : int = f = nil
  type bar = {}
  function empty(b: bar) : int = b = nil
in
  empty(foo {});
  empty(bar {});
  empty(nil)
end

File 4.46: over-amb.tig

$ tc -XO over-amb.tig
error→over-amb.tig:9.3-12: nil ambiguity calling `empty'
error→matching declarations: 
error→    empty @ 
error→    {
error→      f : foo = 
error→      {
error→      }
error→    }
error→    empty @ 
error→    {
error→      b : bar = 
error→      {
error→      }
error→    }
⇒5

Example 4.61: tc -XO over-amb.tig

The spirit of plain Tiger is kept: a “chunk” is not allowed to redefine a function with the same signature:

let
  function foo(i: int) = ()
  function foo(i: int) = ()
in
  foo(42)
end

File 4.47: over-duplicate.tig

$ tc -XO over-duplicate.tig
error→over-duplicate.tig:3.3-27: function complete redefinition: foo
error→over-duplicate.tig:2.3-27: first definition
⇒5

Example 4.62: tc -XO over-duplicate.tig

but a signature can be defined twice in different blocks of function definitions, in which case the last defined function respecting the calling signature is used..

let
  function foo(i: int) = ()
in
  let
    function foo(i: int) = ()
  in
    foo(51)
  end
end

File 4.48: over-scoped.tig

$ tc -XOBA over-scoped.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x55818121a110 */() =
  (
    let
      function foo /* 0x55818121bed0 */(i /* 0x55818121ae00 */ : int /* 0 */) =
        ()
    in
      let
        function foo /* 0x55818121a230 */(i /* 0x55818121a0a0 */ : int /* 0 */) =
          ()
      in
        foo /* 0x55818121a230 */(51)
      end
    end;
    ()
  )

Example 4.63: tc -XOBA over-scoped.tig


Next: , Up: TC-A   [Contents][Index]