Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing multiple modules that have the same named functions #1945

Closed
aleks-qq opened this issue Jun 27, 2019 · 2 comments

Comments

@aleks-qq
Copy link

commented Jun 27, 2019

Quick Summary: Elm lets you import 2 modules and use a function with the same name from both modules.

SSCCE

-- first.elm
module First exposing (number)

number =
 	1


-- second.elm
module Second exposing (number)

number =
 	2


 -- main.elm
import First exposing (number)
import Second exposing (number)

add : Int -> Int
add num =
 	num + number


mynumber =
	add 3 -- mynumber is now 5
  • Elm: 0.19
  • Browser: Chrome
  • Operating System: Mac

Additional Details

I feel this could be dangerous and confusing with a bigger codebase that use many modules.
I'm pretty new to elm and I know that my example isn't the most kosher one but maybe the compiler shouldn't let you use duplicate function names especially if they return the same type?

@evancz evancz added the bug label Jul 2, 2019

@evancz

This comment has been minimized.

Copy link
Member

commented Jul 2, 2019

Thank you for the nice SSCCE! It looks like this is a regression. With 0.18 I am getting:

-- NAMING ERROR -------------------------------------------------------- bug.elm

This usage of variable `number` is ambiguous.

6|   num + number
           ^^^^^^
Maybe you want one of the following?

    First.number
    Second.number

Looking into it further now!

@evancz evancz closed this in 82a8be3 Jul 2, 2019

@evancz

This comment has been minimized.

Copy link
Member

commented Jul 2, 2019

Based on the change in 82a8be3 I am now seeing the following error:

-- AMBIGUOUS NAME ------------------------------------------------------ bug.elm

This usage of `number` is ambiguous:

6|   Html.text <| String.fromInt number
                                 ^^^^^^
This name is exposed by 2 of your imports, so I am not sure which one to use:

    First.number
    Second.number

I recommend using qualified names for imported values. I also recommend having
at most one `exposing (..)` per file to make name clashes like this less common
in the long run.

Note: Check out <https://elm-lang.org/0.19.1/imports> for more info on the
import syntax.

So this should be available once 0.19.1 is ready for release.


Before the patch, this regression manifested in only some cases of ambiguous imports:

import First exposing (..)
import Second exposing (..)
-- GOOD gives error

import First exposing (number)
import Second exposing (..)
-- GOOD gives error

import First exposing (..)
import Second exposing (number)
-- BAD no error

import First exposing (number)
import Second exposing (number)
-- BAD no error

So I hope that this regression is relatively rare in practice since 0.19 came out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.