## Importing a Tooldef script

Assume you already wrote a ToolDef script with some tools. For example:

    // incdec.tooldef

    tool int inc(int v):
      return v + 1;
    end

    tool int dec(int v):
      return v - 1;
    end

Arguably, these are not the most interesting tools, but suppose you want to use the `inc` tool in another ToolDef script. Obviously, one way to do that is to copy the entire tool to the other script. But then you have two copies of the same tool in two different scripts, and keeping them consistent with each other can be complicated, especially when you have many tools or many copies.

The alternative is to import the `incdec.tooldef` script into the other ToolDef script, by

    import "incdec.tooldef";

    outln("Result: %d", incdec.inc(1));

The import makes all tools of the imported script available under the name of the imported file. In the above example, after the `import "incdec.tooldef"`, the tools `incdec.inc` and `incdec.dec` are available and they can be used like any other tool. In the example, the first tool from `incdec.tooldef` is executed.

### Importing a Tooldef script with renaming the script name

As you can see above, the import uses the `incdec` name of the imported script. That may cause trouble if a script already uses that name for a different purpose. The solution is to supply a different name to the import statement:

    import "incdec.tooldef" as imported;

    tool int incdec(int a):
      ...
    end

    outln("Result: %d", imported.inc(1));

The import statement is extended with `as imported`. This means that the tools in the `incdec.tooldef` script will be made available under the `imported` name instead of under the `incdec` name. That makes the `incdec` name available for use by the local `tool int incdec(int a)` tool.

### Importing a ToolDef tool

In the import examples above, complete ToolDef scripts were imported. Tools inside the script then become available in the imported script. The advantage of this behavior is that it is easy to understand where a tool is defined in the ToolDef files. This is particularly useful in large or complicated ToolDef scripts.

However, sometimes it can be useful to be able to directly import a tool itself rather than the script containing it. This can be achieved with the `from` import form, like:

    from "incdec.tooldef" import inc;

    outln("Result: %d", inc(1));

In the example, the `inc` tool becomes available by its own name, as if you defined it locally. The import did not mention the `dec` tool. This tool is therefore not available after the above import.

You can however import more than one tool at the same time, by listing all tools to import. For example:

    from "incdec.tooldef" import inc, dec;

This imports both the `inc` and the `dec` tool from the `incdec.tooldef` script.

To avoid problems with trying using the same name for more than one tool, the `from` form of import also allows renaming a tool during import. For example:

    from "incdec.tooldef" import inc as add_one;

This line imports the `inc` tool, but the imported tool is additionally renamed to `add_one`.

Finally, the `from` form has a shortcut to import all tools from a script:

    from "incdec.tooldef" import *;

This import line makes all tools in `incdec.tooldef` available. The advantage of the `*` import is that the import line is very short. The disadvantage of importing tools in this way is that the line now does not explicitly list what is being imported. If you import several scripts in this way, it may become hard to understand what exactly was imported and where a tool is imported from. Therefore, use of the `*` import is generally discouraged for larger scripts and for scripts with many imports.