Elixir: Types defined in a module are not visible in other module after import.

Created on 22 Jul 2014  路  6Comments  路  Source: elixir-lang/elixir

Simplest example:

defmodule Foo do
  @type t :: String.t
end

defmodule Bar do
  import Foo

  @spec quux(t) :: any
  def quux(x), do: x
end 
% mix
lib/foo.ex:6: warning: unused import Foo

== Compilation error on file lib/foo.ex ==
** (CompileError) lib/foo.ex:8: type t() undefined
    (stdlib) lists.erl:1336: :lists.foreach/2
    (stdlib) erl_eval.erl:657: :erl_eval.do_apply/6
    (elixir) src/elixir.erl:170: :elixir.erl_eval/3
    (elixir) src/elixir.erl:158: :elixir.eval_forms/4
    (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/2
    (elixir) src/elixir.erl:170: :elixir.erl_eval/3

Elixir 0.14.3

(of course in the real world equivalent of Foo is not only used for defining types).

Most helpful comment

It could potentially be fixed, if we wanted, by storing the type information but I am not really sure if we should. Maybe by an implicit import type directive, I wouldn't mix it with the regular import though, since it increases the chances of conflicts and so on.

All 6 comments

We can't import types because they are not available to us at that point. import only works for functions and macros. You have to reference the remote type with it's module name (which can be aliased).

Thx for the explanation. Is there any remote possibility it will change in the future?

It's a technical limitation that I don't think we can work around (I could of course be wrong). Unfortunately types are not available to us for modules loaded during compilation.

It could potentially be fixed, if we wanted, by storing the type information but I am not really sure if we should. Maybe by an implicit import type directive, I wouldn't mix it with the regular import though, since it increases the chances of conflicts and so on.

Any chance this might get implemented after all these years? or it is a topic to be closed?

_Example:_
@type html_tree :: Floki.html_tree
Simply redeclare types you would like to use without a module name.
The topic is closed but I am writing this as a tip for who would still search the topic.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josevalim picture josevalim  路  3Comments

DEvil0000 picture DEvil0000  路  3Comments

lukaszsamson picture lukaszsamson  路  3Comments

cmeiklejohn picture cmeiklejohn  路  3Comments

andrewcottage picture andrewcottage  路  3Comments