bodhilogic
Is the Pin operator backwards?
Elixir is touted as being a ‘Functional Language’ and the common characteristic of a functional language is that its ‘variables’ are immutable.
Other functional languages allow some tolerance for mutability, as long as some explicit ceremony is employed so that the developer knows he/she is ‘breaking the rules’.
it seems to me that the Pin operator should really be an ‘UnPin’ operator so that allowing a left side to be re-matched to a right side becomes an explicit operation i.e. the left side is immutable unless I use the ‘UnPin’ symbol - “I know that by writing ‘^a’ I am explicitly allowing a state change”.
Does this make sense or do I misunderstand something?
I’m not putting Elixir down - just trying to understand it.
Yes, I have read José’s github posting about why he allows re-matching. I am just thinking that allowing re-matching shouldn’t be the default behavior and that it doesn’t cost much time or effort to type one extra character to allow the re-match.
Most Liked
michalmuskala
How would that work in expressions where you bind some variables and match on others? Which operator would you use and why?
Also, what about pattern matching in case or function heads, where you don’t have the operator at all, yet still have the matching/binding behaviour.
benwilson512
Rebinding a variable does not mutate data. If I have:
x = %{foo: 1}
y = x
x = Map.put(x, :bar, 2)
y is still %{foo: 1}. The data itself is completely unchanged. We have shadowed x, but that isn’t the same thing as mutability. Data and bindings to data aren’t the same thing. It can be a difficult thing to see at first for some, but the “tolerance for mutability” you speak of from other functional languages doesn’t even apply here, since mutable data is not a feature available in the VM.
This is perhaps easier to see in a loop:
x = 1
for i <- 1..10 do
x = i
end
If = were performing a mutation, x would be 10, but in fact it’s still just 1. All that happens is shadowing / rebinding, and that naturally doesn’t work across lexical scopes.
bodhilogic
Fantastic responses!
Thank you all so much.
I suppose I just need to “retrain the brain” from a world of imperative programming to that of functional programming.
I had to go through a similar shift many, many years ago coming from iterative programming to the world of Prolog - you just have to learn to think differently ;).
I suppose a year from now, I’ll look back and laugh at myself for not appreciating the syntactic sugar that José took the time to build into Elixir.
Having said all that, I am really looking forward to exploring Elixir and becoming fluent in a ‘new’ way to program!
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









