# Why doesn't ITK SwigInterface/igenerator.py just use SWIG \`%template\`?

**URL:** <https://discourse.itk.org/t/why-doesnt-itk-swiginterface-igenerator-py-just-use-swig-template/7377>\
**Category:** Engineering\
**Created:** [January 6, 2025, 9:39pm UTC](https://discourse.itk.org/t/why-doesnt-itk-swiginterface-igenerator-py-just-use-swig-template/7377 "2025-01-06T21:39:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [January 6, 2025, 9:39pm UTC](https://discourse.itk.org/t/why-doesnt-itk-swiginterface-igenerator-py-just-use-swig-template/7377/1 "2025-01-06T21:39:16Z")

</div>

In order to create a Python wrapping for ITK, [Wrapping/Generators/SwigInterface/igenerator.py](https://github.com/InsightSoftwareConsortium/ITK/blob/2fd36ab3a6871340c1defd5a69dc657a81bb8134/Wrapping/Generators/SwigInterface/igenerator.py) generates “itk\*.i” files for all wrapped ITK types. For example for `itk::Size<unsigned int>`, it generates a file named `<Bin>\ITK\Wrapping\Typedefs\itkSize.i`. This file contains class definitions for itkSize2, itkSize3, and itkSize4, for example (simplified):

```cpp
class itkSize2 {
  public:
    ...
    itkSize2();
    itkSize2(itkSize2 const & arg0);
    itkSize2 & operator=(itkSize2 const & arg0);
    ~itkSize2();
};

```

These classes (itkSize2, itkSize3, and itkSize4) are then passed to SWIG, in order to wrap them, if I understand correctly.

Is there a specific reason why we do it like that? Instead of passing those classes to SWIG, would it be an option to directly pass the `itk::Size<unsigned int>` template to SWIG? SWIG does support templates, for example:

```cpp
%template(itkSize2) itk::Size<2>;
%template(itkSize3) itk::Size<3>;
%template(itkSize4) itk::Size<4>;

```

So why don’t we use the SWIG `%template` keyword?

See also the discussion I had with @blowekamp at [How to wrap a static constexpr data member (an integer value), for Python? · swig/swig · Discussion #3088 · GitHub](https://github.com/swig/swig/discussions/3088#discussioncomment-11753528)

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [January 7, 2025, 2:21am UTC](https://discourse.itk.org/t/why-doesnt-itk-swiginterface-igenerator-py-just-use-swig-template/7377/2 "2025-01-07T02:21:44Z")

</div>

We do use `%template` for STL types [in _pyBase.i_](https://github.com/InsightSoftwareConsortium/ITK/blob/3f762db8334c8d5919b5b37687a1fd4848452503/Wrapping/Generators/Python/PyBase/pyBase.i#L4).

To use `%template`, it may be challenging to implement the rest of the functionality in _pyBase.i_ and similar functionality.

To go in that direction, we would want to transition ITK to more STL functionality and behavior and then use SWIG’s built-in support for what was implemented manually. For example,

- `itk::LightObject` → `std::shared_ptr`
- `itk::Size` → `std::array`

etc.

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [January 7, 2025, 12:34pm UTC](https://discourse.itk.org/t/why-doesnt-itk-swiginterface-igenerator-py-just-use-swig-template/7377/3 "2025-01-07T12:34:38Z")

</div>

> [@matt.mccormick](#):
>
> We do use `%template` for STL types [in _pyBase.i_](https://github.com/InsightSoftwareConsortium/ITK/blob/3f762db8334c8d5919b5b37687a1fd4848452503/Wrapping/Generators/Python/PyBase/pyBase.i#L4).

Thanks Matt! So we use `%template` for STL types, but not for ITK types. Why not for ITK types?

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [January 7, 2025, 2:12pm UTC](https://discourse.itk.org/t/why-doesnt-itk-swiginterface-igenerator-py-just-use-swig-template/7377/4 "2025-01-07T14:12:06Z")

</div>

ITK types have their own ownership semantics and mechanisms, code to behave like Python sequences when appropriate, etc.

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [January 7, 2025, 4:27pm UTC](https://discourse.itk.org/t/why-doesnt-itk-swiginterface-igenerator-py-just-use-swig-template/7377/5 "2025-01-07T16:27:12Z")

</div>

An advantage of using SWIG `%template` would be automatic wrapping of static constexpr data members. For example, the static data member `itk::Size::Dimension` would then be wrapped automatically into Python as a Python _class variable_. It seems to me that it is technically possible to use `%template` for “simple” ITK types like `itk::Size` and `itk::Point`. But anyway, it may still be a lot of work 🤷
