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

Modules cannot have ports with same name #1450

Open
b123400 opened this Issue Jul 26, 2016 · 5 comments

Comments

Projects
None yet
7 participants
@b123400

b123400 commented Jul 26, 2016

Elm version: 0.17.0

I have two modules, they both have a port with the same name:

port module ModuleA exposing (main)

import Html exposing (text)
import Platform.Sub as Sub

port incomePort : (String -> msg) -> Sub msg

main =
  text "Hello"
port module ModuleB exposing (main)

import Html exposing (text)
import Platform.Sub as Sub

port incomePort : (String -> msg) -> Sub msg

main =
  text "Hey!"

And I compiled it by this command:

elm make ModuleA.elm ModuleB.elm --output combined.js

The generated JS file will output the following error:

Error: There can only be one port named `incomePort`, but your program has multiple.

These two modules are not connected and there should be no ambiguity between the ports. I think the ports name checking should be bounded to the module, instead of JS-file-wise.

By the way, when I generate the modules separately and include them in the same page, there is no error.

elm make ModuleA.elm --output a.js
elm make ModuleB.elm --output b.js
<script type="text/javascript" src="a.js"></script>
<script type="text/javascript" src="b.js"></script>

Is this behaviour intended? I can try to send a PR to fix it if it is not :)

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Jul 26, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Jul 26, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@evancz evancz added the ports label Sep 21, 2016

@sachin-bs

This comment has been minimized.

Show comment
Hide comment
@sachin-bs

sachin-bs Nov 6, 2017

bump, any updates?

sachin-bs commented Nov 6, 2017

bump, any updates?

@kachkaev

This comment has been minimized.

Show comment
Hide comment
@kachkaev

kachkaev Dec 6, 2017

Just raised a similar concern on stackoverflow. Being a newcomer in Elm, here is what I wanted to do to avoid import glitches:

import MyModule exposing (myFunction as myRenamedFunction, somethingElse)

Having that supported by the compiler would be awesome!

kachkaev commented Dec 6, 2017

Just raised a similar concern on stackoverflow. Being a newcomer in Elm, here is what I wanted to do to avoid import glitches:

import MyModule exposing (myFunction as myRenamedFunction, somethingElse)

Having that supported by the compiler would be awesome!

@rgrempel

This comment has been minimized.

Show comment
Hide comment
@rgrempel

rgrempel Mar 13, 2018

These two modules are not connected and there should be no ambiguity between the ports.

But think about the way you use the ports, on the Javascript side. You'll end up saying stuff like:

var elmApp = Elm.Main.fullscreen({
   ...
});

elmApp.ports.cacheCredentials.subscribe(function(params) {
   ...
}

Now, which cacheCredentials does this refer to, if the port name is used in two different modules?

Of course, there would be a way to solve this problem -- you could imagine elmApp.ports exposing a port name qualified by the module name, for instance -- I just wanted to point out that it's not a trivial problem to fix. Or, in other words, there is currently an ambiguity, even if you could imagine deeper changes that might avoid it.

rgrempel commented Mar 13, 2018

These two modules are not connected and there should be no ambiguity between the ports.

But think about the way you use the ports, on the Javascript side. You'll end up saying stuff like:

var elmApp = Elm.Main.fullscreen({
   ...
});

elmApp.ports.cacheCredentials.subscribe(function(params) {
   ...
}

Now, which cacheCredentials does this refer to, if the port name is used in two different modules?

Of course, there would be a way to solve this problem -- you could imagine elmApp.ports exposing a port name qualified by the module name, for instance -- I just wanted to point out that it's not a trivial problem to fix. Or, in other words, there is currently an ambiguity, even if you could imagine deeper changes that might avoid it.

@fdbeirao

This comment has been minimized.

Show comment
Hide comment
@fdbeirao

fdbeirao Mar 14, 2018

Contributor

I just want to post here that this talk "The Importance of Ports" by Murphy Randle has helped me (YMMV) a lot when using ports in my Elm apps. I now have one single Ports.elm module, which is the only port module and which contains two ports: infoForOutside and infoForElm. This mitigates/works around the ambiguity raised in this issue.

Contributor

fdbeirao commented Mar 14, 2018

I just want to post here that this talk "The Importance of Ports" by Murphy Randle has helped me (YMMV) a lot when using ports in my Elm apps. I now have one single Ports.elm module, which is the only port module and which contains two ports: infoForOutside and infoForElm. This mitigates/works around the ambiguity raised in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment