3
[5,a,15] = [5,10,15]
Doing this type of assignment in Elixir is Duck Typing
or are different concepts?
I ask because I was left with doubt since on the left side is declared a "variable" without an explicit type or a direct assignment value.
3
[5,a,15] = [5,10,15]
Doing this type of assignment in Elixir is Duck Typing
or are different concepts?
I ask because I was left with doubt since on the left side is declared a "variable" without an explicit type or a direct assignment value.
5
You’re assigning a list with 3 values to a list with elements, two of them are values, so it doesn’t make sense to use it that way. The only element that serves any purpose is the variable a
. Then 10 will be assigned to a
and you can use this variable later with this value.
Elixir actually defines it as Pattern matching at all times.
The variable a
does not have a type defined because Elixir is a language of dynamic typing. The value it holds is that it has type. This type is defined according to the syntax used, so it is inferred by its structure.
Behold more about typing.
Then the second element of the list on the left is a variable that will store the value that is in the second element of the list on the right. Its type is an integer inferred by the way it is structured (written).
Elixir does not say it implements Duck Typing. Good, I respect that.
I don’t really like the term. It seems that no one knows what this means in fact and each one has its own version, most of them add nothing to what already exists and do not indicate that this has to do with the typing system. So it’s best to forget that term, which is funny, but serves no purpose in engineering terms.
Anyway nothing indicates to be using it according to most definitions.
0
The concept of Duck Typing is as follows: "if it makes Quack like a duck, then it’s a duck". This concept is widely used in languages with dynamic typing for code reuse (DRY - don’t repeat yourself), but better known in the Ruby community because of module features.
But what does that mean?
Imagine you have a function that gets a value unprincipled and does some operation (even if it is a print) on that value:
module Printer
def print
self.to_s
end
end
This module by itself in Ruby does nothing but when included (include) in a class, all classes that make this inclusion will "respond to" (ans_to) method print.
The self in the method is with no restriction as to the type (class) where this module can be inserted, so it will Duck Typing.
To better understand the Duck Typing in this example, consider the following sentence: "if print as a Printer, then it’s a Printer".
class Car
include Printer
end
class Person
include Printer
end
Car and Person are completely different types and so without the Duck Typing would not be possible something like:
def show(car_or_person)
car_or_person.print
end
To do this in Java, which is a language with static typing, we could do two things:
...
public void show(Car car) { car.print(); }
public void show(Person person) { person.print(); }
...
or
public abstract class Printer {
public void print() {
System.out.println(this.toString());
}
}
public class Car extends Printer {}
public class Person extends Printer {}
...
public void show(Printer printer) { printer.print(); }
However Duck Typing is more associated with languages with dynamic typing, so the code looks better in Ruby or Elixir (through protocols).
Browser other questions tagged typing erlang elixir
You are not signed in. Login or sign up in order to post.
Very good, thanks, I just talked about her not having an explicit type to make it clear. I know about the concept of dynamic language and the Duck Type ( Which you explained in another question that I read ha ) ... I asked because I watched a talk recently and the presenter came across this question.
– Luis Souza
When it’s like that. Put the link to give context.
– Maniero
Yes, I would if I had ... I watched it on a colleague’s computer and I don’t remember the title, I will try to search and add on this and future questions.
– Luis Souza