3 ms·
> 2) my number one wish-that-will-never happen for elixir would be the inverse of the mutability issue, variables should be immutable by default but have a sigi
by losvedir 8y ago
> 2) my number one wish-that-will-never happen for elixir would be the inverse of the mutability issue, variables should be immutable by default but have a sigil or postfix (like !) that lets you make it mutable.
Just to clarify for others, and as the article notes, variables aren't "mutable" in Elixir but rather can be rebound. Even rust, where - just like you desire - you have to explicitly tag something "let mut" if you want mutability, will let you re-bind the variable without that tag.
I've programmed in Elixir full time now for two years and I can't recall any times accidentally shadowing a variable has bit me (but I do `mix compile --force --warnings-as-errors`) as part of my development routine, and that has caught it before for me.
(Oh, unless you mean you wish Elixir had _real_ mutability. That _would_ be quite the change, as obviously no functions in the standard library support that and you have to make do with GenServers and the like. I'm not sure I'd like that as the whole language ecosystem is predicated on immutability and message passing...)
While we're throwing out "one wish-that-will-never happen" ideas, though, here's mine: I'd love for Elixir to be able to automatically add guards to functions based on the type spec. That is, if you had this code:
@spec foo(String.t(), MyEnum.t()) :: boolean()
def foo(str, enum) do
# ...
end
I would love it if that got compiled to essentially:
def foo(str, enum)
when is_binary(str) and enum in [:enum1, :enum2, ...] do
# ..
end
In general, dialyzer is the weakest part of the ecosystem for me, and I've had `nil` and other random things sneak into my functions before despite being against the type spec. I'd much prefer to have the function call crash early by just not matching any clauses.
- dnautics 8y agoYes sorry I was being inexplicit about variable rebinding. I'm used to Julia where variable refers to the lexical form.... Identifier? And not to the actual underlying data, which is called the value. I'll write up my credo linter and see if you like it. Your guard idea is pretty good, but I would appreciate it even more if those guards existed in dev and test and got yanked in prod.