[Proposal] More intuitive import shadowing

58 views
Skip to first unread message

Krzysztof Wende

unread,
Sep 21, 2017, 10:17:12 AM9/21/17
to elixir-lang-core
Right now when we import a module A

defmodule CurrentA do
 
def public1, do: 1
 
def public2, do: 2
 
def public3, do: 3
end


defmodule
CurrentA do
 
import CurrentA, only: [public1: 0]
 
import CurrentA
 
 
def test, do: public2() # No problem whatsoever
end

But whenever we add `except` to it

defmodule CurrentA do
 
def public1, do: 1
 
def public2, do: 2
 
def public3, do: 3
end


defmodule
CurrentA do
 
import CurrentA, only: [public1: 0]
 
import CurrentA, except: [public3: 0]
 
 
def test, do: public2() # undefined function public2/0
end

We get an error of inexistent function. For me it feels super counter-intuitive.

Thoughts?
Cheers,
Krzysztof

Ben Wilson

unread,
Sep 23, 2017, 8:29:19 AM9/23/17
to elixir-lang-core
Setting aside whether or not the existing functionality is intuitive or not, this isn't actually a proposal. You aren't suggesting an alternative.

José Valim

unread,
Sep 23, 2017, 8:46:08 AM9/23/17
to elixir-l...@googlegroups.com
This is also by design and mentioned in the docs. Except is always exclusive on a previously defined import, if there is no import, then we take all macros and functions.

Imagine you "import Foo.Bar, only: ..." and then in some part of your code you have a conflict and would you like to remove that conflict in particular. If except brought everything back, it would be confusing behaviour and the only alternative would be for you to repeat the whole list in :only except for the element you want removed.

José Valim
Founder and 
Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/7f1e33c3-21d4-4c0f-b176-bdcc1fe35f81%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Krzysztof Wende

unread,
Sep 24, 2017, 12:49:09 PM9/24/17
to elixir-lang-core
Ok, 

But now let's imagine this scenario:


defmodule MyHelper do
  defmacro __using__
(_) do
    quote
do
     
import List, only: [foldl: 3]
   
end
 
end

end
defmodule A
do
 
use MyHelper
 
import List, except: [zip: 1]
 
 
def test, do: foldr([1,2,3], 10, &(&1 + &2))
end

We'd obviosly expect in module A to have all of the List functions except zip, but we actually get none except for foldl.

José Valim

unread,
Sep 24, 2017, 1:19:39 PM9/24/17
to elixir-l...@googlegroups.com
You can still undo it by bringing all of List and then using except. So you have an option to bring it all in once again. If it behaved like you proposed, then it would be impossible to filter without fully repeating the previous declaration.



José Valim
Founder and 
Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages