Why are import statements needed?

Would it be possible to implement implicit importing in Elm? It should be able to know that any capitalized name followed by a dot is a module name

like when i put Module.thing anywhere in my code it could just implicitly import Module

Because module names are tied to their path in the filesystem, this is technically achievable. In fact this is what OCaml already does with it’s modules.

I expect this is likely a design decision rather than a technological limitation. Being explicit about what dependencies are being pulled into a module makes it much easier to read code. It’s the same reason why we favour qualified imports vs import Thing exposing (..).

2 Likes

As someone who has struggled with explicit vs implicit outside and inside programming their whole life, I very much appreciate Elm’s explicitness.

3 Likes

I wonder if the compiler has performance optimizations (as in how fast it can compile your project) based on the fact that it only needs to parse the first bit of each file to get to know the entire dependency tree?

1 Like

You are absolutely right, it does!

I delved into the compiler source for my thesis. It generates a dependency tree quicker by only parsing imports. Then it can parallelize actual parsing and type checking on independent modules.

14 Likes

What if I have these modules:

  • Main
  • Foo.Bar
  • Foo

and I do:

module Main exposing (f)

import Foo.Bar as Foo

f = Foo.g 42

What does Foo.g mean? Is it function from Foo.Bar as Foo or from Foo. I don’t think how this improves readability. I also have some experience working in OCaml and implicit imports are easily the thing I hate most about it.

3 Likes

Not really the same topic, but maybe addressing the same need: the Intellij Elm plugin can automatically import things. So like, if I want Module.thing, I dont type import Data.Module as Module first, I just type Module.thing and the plugin asks me if I want to import Data.Module as Module.

I love this feature. I have sort of forgotten how to import things manually.

But, its still really handy to have import statements. I still read them a lot, even if I dont have to type them.

3 Likes

Well, ReasonML/Ocaml seems to simply shadow the original Foo inside Main in a similar situation. So that’s one solution.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.