I would be all for changing the way Kernel is imported to allow a name that 
exists in Kernel to be redefined (potentially generating a warning), 
because regardless of what name we choose, if this behaviour is not altered 
then adding any function to Kernel will be a breaking change.

On the other hand: I do not think this is the first time that a new 
function is added to kernel after 1.0 (or is it)? What approach was taken 
in those earlier cases?

~Marten/Qqwy

On Wednesday, July 3, 2019 at 2:16:22 PM UTC+2, Alexis Brodeur wrote:
>
> While this is a great idea, and I am overall in favor of an 
> `Kernel.identity/1`, let me play devil's advocate for a bit...
>
> According to lib/elixir/src/elixir_locals.erl 
> <https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_locals.erl#L92>,
>  
> all `def`s in a module are validated to not clash against an already 
> imported function from other modules.  By introducing `Kernel.identity/1`, 
> modules in the wild already using a `identity/1` function would cease to 
> compile, therefore making this change a breaking one, according to my 
> current understanding of the compiler.
>
> A simple `test.exs` module
> ```ex
> defmodule Test do
>   def test do
>     inspect("term")
>   end
>
>   def inspect(term, opts \\ []) do
>     IO.puts("my inspect/2")
>   end
> end
>
> Test.test
> ```
>
> against the current `master` of elixir yields:
> ```
> == Compilation error in file /Users/brodeuralexis/test.exs ==
> ** (CompileError) /Users/brodeuralexis/test.exs:6: imported 
> Kernel.inspect/1 conflicts with local function
>     (elixir) src/elixir_locals.erl:94: 
> :elixir_locals."-ensure_no_import_conflict/3-lc$^0/1-0-"/2
>     (elixir) src/elixir_locals.erl:95: anonymous fn/3 in 
> :elixir_locals.ensure_no_import_conflict/3
>     /Users/brodeuralexis/test.exs:1: (file)
> ```
>
> On Tuesday, July 2, 2019 at 5:23:47 AM UTC-4, Alexey Nikitin wrote:
>>
>> Like Haskell's `id` or Clojure's `identity` or Ruby's `itself`
>>
>> It can be very useful sometimes. Instead of writing ugly `&(&1)` it would 
>> much more attractive to write `&identity/1` or `&id\1`
>> Moreover this function is already used as a default argument for some 
>> higher-order function. For example `Enum.group_by(enumerable, key_fun, 
>> value_fun \\ fn x -> x end)`
>>
>> Here are some examples
>>
>> ```
>> #=> [1, 2, 3, true, 1234] 
>>
>> 'abcdaabccc' |> Enum.sort |> Enum.chunk_by(&identity/1) 
>> #=> ['aaa', 'bb', 'cccc', 'd'] 
>>
>> [1, 1, 2, 3, 3, 1, 1, 5, 5] |> Enum.chunk_by(&identity/1) |> 
>> Enum.map(&hd/1) 
>> #=> [1, 2, 3, 1, 5] 
>>
>> Enum.group_by('abracadabra', &identity/1) 
>> #=> %{97 => 'aaaaa', 98 => 'bb', 99 => 'c', 100 => 'd', 114 => 'rr'} 
>>
>> Enum.map([1, 2, 3, 4], &identity/1) 
>> #=> [1, 2, 3, 4]
>> [1,2,3,nil, true, false, 1234] |> Enum.filter(&identity/1) 
>> ```
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/f87de323-20b7-4616-843c-10863a4c079d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to