Is it possible to import types so as to leave off `module_name` from `module_name:type_name()`?

I currently understand how to define custom types and export them, as described here. For example:

-module(common_types).
-export([my_type/0]).
-type my_type() :: string().

When using my_type in a spec in another module, I can use it like:

-spec some_function(common_types:my_type()) -> any()

I would like to import the type such that I can just do:

-spec some_function(my_type()) -> any().

This would allow me to name my modules containing types in a non-abbreviated way and then import them to clarify where the custom types are coming from. If I have to do common_types:my_type every time, my worry is that this is going to get verbose. So, then I’ll have to do something like ctypes:my_type, which I would prefer not to do.

I have tried using:

-import(common_types, [my_type/0]).

but this doesn’t seem to work.

Apologies if I have missed anything in the documentation! I have looked!

1 Like

I don’t think it is possible to import, but you could declare a local alias, which would have the safe effect I think. That is:

-type my_type() :: common_types:my_type().
7 Likes

You could just have those types in a header file (.hrl) and include that. No module name required then.

2 Likes

Thank you both for the two options!

1 Like

That makes things very annoying when the header changes. If you use local types, you only need to recompile the “main” module that contains the for the change to get picked up, not all modules that use the header.

1 Like