Iex Shell: How to delete/overwrite an alias in the shell?

I have asked this question on Slack, no solutions from there, so putting it up here.

The situation is,

Let’s say I’ve typed in the iex shell alias Asdf.Qwer, but actually what I need now is simply Qwer , I have no way to clear Asdf.Qwer because alias Qwer just again gives me Asdf.Qwer . So am I forever stuck with that? Or is there a way to reset/delete aliases

In other words, Qwer is now forever referring to Asdf.Qwer because of my mistype, and there is no way to delete/say that I don’t want this anymore. Or is there?

(P.S. an unrelated question, is the source code of Elixir Forum available as open source somewhere? It’s a great interface and I admire it very much.)

1 Like

Is closing and reopening the shell (or via respawn) not a viable solution? (basically it will clear all bound variables and aliases in the shell.)

1 Like

Thanks, but I don’t want to clear all variable bindings I have currently declared so far and am working on something. (Otherwise just ctrl-C twice out from the shell and coming in back again would have been done.)

Reason I’m asking is to see whether this possibility is already there, just that I don’t know the way how-to.

And Erlang’s shell did have an f() / f(variable) function that cleared the binding (for all/a variable), so I just want to find out if there’s something along the same idea in Iex.

It may be the aliases are stored in some (state-holding) variable or struct somewhere, like in Python-world, ‘sys.modules’ or such. But I don’t know.

Since you use alias which is alias Asdf.Qwer, you can refer to the original Qwer using Elixir. Call it like this Elixir.Qwer.

1 Like

Hmm, funny, it seems that you can overwrite alias in some cases and can’t in other cases. I’ve played with this:

iex> alias Asdf.Qwer
Asdf.Qwer
iex> Qwer
Asdf.Qwer

# still not working
iex> alias Qwer
Asdf.Qwer
iex> alias Qwer, as: Qwer
Asdf.Qwer

# this works?
iex> alias Foo, as: Qwer
Foo
iex> Qwer
Foo

# this also works
iex> alias Bar.Qwer
Bar.Qwer
iex> Qwer
Bar.Qwer

# funnily, this seemed to work at first glance (but really doesn't)
iex> alias Elixir.Qwer
Qwer # refers Qwer, yeay!
iex> Qwer
Bar.Qwer # it reverts, the alias doesn't work.
2 Likes

This is a bug. Can you please file a bug report?

2 Likes

Yeah, I can confirm this. I also just playing around with alias in IEx with the same result as yours.

1 Like

Will do!

1 Like

https://github.com/elixir-lang/elixir/issues/5587

1 Like

Thanks guys. Just saw the replies as I’ve been away for the holidays.

@bobbypriambodo’s tests and attempts did make sense but ultimately discovered that it couldn’t behave as expected or intended; I tried and confirmed that too.

# originally already before this `alias Asdf.Qwer`
iex(42)> Qwer
Asdf.Qwer 
iex(43)> alias Elixir.Qwer, as: Qwer
Qwer      
iex(44)> Qwer
Asdf.Qwer 
iex(45)> alias Foo, as: Qwer
Foo       
iex(46)> Qwer
Foo       
iex(47)> Elixir.Qwer
Qwer      
iex(48)> alias Elixir.Qwer, as: Qwer
Qwer      
iex(49)> Qwer
Foo 

So logging as a bug does appear to make the most sense. Thanks again everybody!! :grinning:

1 Like

Anytime :smile:

1 Like