Using the @inferred macro

Julia comes with a handy macro in the Test package that can be used to check whether the return type of a function matches the inferred return type of the function. The inferred return type is simply the type that we see from the @code_warntype output before.

For example, we can check the notorious random_data function from the beginning of this section:

The macro reports an error whenever the actual returned type differs from the inferred return type. It could be a useful tool to validate the type instability problem as part of an automated test suite in the continuous integration pipeline.

The primary reason to use a barrier function is to improve performance where the type instability problem exists. If we think about it more deeply, it also has the side benefit of forcing us to create smaller functions. Smaller functions are easier to read and debug and perform better.

We have now concluded all patterns in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.168.56