Duck Printing and General Programming

I searched for SO after a while, and I could not find a definite and general answer, only some conflicting and specific opinions. [1]

So, I would like to know what is the relationship between duck typing and general programming? (DT <GP, DT == GP, DT> GP). In general programming, I mean, in particular, C ++ templates or generalized Java files, but a general answer related to concepts, if possible, would be welcome.

I know that general programming will be processed at compile time, while duck printing will be processed at runtime, but in addition to this I do not know how to position them.

Finally, I do not want to start a debate, so I would prefer answers as reasons, reasons against.

[1] What is the relationship between the C ++ pattern and duck print?

+4
source share
4 answers

I came across two different definitions of "Duck Typing." Sure, there are people who will tell you that one of them is “right” and the other is “wrong.” I'm just trying to write that they are both used, and not telling you that they are both “correct”, but personally I don’t see anything bad in a broader sense.

1) input only at run time. A type is a property of objects, not variables, and therefore, it is mandatory that when you come to a method call on an object or otherwise use properties that it has due to its type, the presence or absence of this method is detected at runtime [*] . So, if it “looks like a duck and quacks, like a duck” (that is, if it has the quack() function), then it “is” a duck (in any case, you can consider it as one). By this definition, of course, C ++ templates fall from the first hurdle, they use static typing.

2) The name used in a more general sense for the principle that if it looks like a duck and a quack, like a duck, then it is a duck, meaning any installation in which interfaces are implicitly determined by the operations performed by the consumer interface, rather than interfaces that are explicitly advertised by the manufacturer (regardless of what the interface implements). By this definition, C ++ templates use some kind of duck print, but is there something defined “like a duck” at compile time, and not at run time, based on its static type and not its dynamic type. "Check compile time, can this variable be of static type quack?", And not "check at runtime, can this object quack?".

The argument, in my opinion, in my opinion, is that Python "owns" the term, so that only systems like Python can be called duck typing, or others can use this term to refer to a similar concept in a different context. No matter what you think it should mean, it seems irresponsible to use the term "jokey" and demand that everyone understand the same formal definition. SO is not a suitable forum to tell you what the term "should" means if there is no authoritative source that you are asking for, for example, a dictionary or any scientific documents that formally define it. I think he can tell you what he really meant.

General programming can use duck printing or not, depending on the exact details. C ++ generic containers use duck printing of type 2 because for a generic type in C ++ it does not guarantee that you can copy it, compare it, get a hash value, etc. No Java containers, Object has enough methods to make it hashable, etc.

Conversely, I suspect that everything you do that uses duck printing can reasonably be called "universal programming." Therefore, I assume that in the terms you requested, GP> DT is that duck typing (or definition) is a strict subset of a wide range of things that can be called "general."

[*] Well, in some cases, your dynamic language may have some static analysis, which proves the case one way or another, but the language requires the ability to postpone this check until execution time for cases where the static analysis cannot finally say.

+6
source

This is really a matter of vocabulary. It has the most general meaning, general programming is not related to the compilation time problem vs. run-time: this is a solution to a common problem. A good example is where general programming is Python runtime, but it is also possible to implement universal programming at runtime in C ++ (at a considerable cost at runtime).

Duck printing is an orthogonal concept and is commonly used to refer to a run-time printout. Again, the most frequently cited modern Python example, but many, many languages, starting with Lisp, have used it in the past. As a rule, both C ++ and Java made an explicit choice not to support duck printing. This is a trade-off: security versus flexibility (or compile-time errors compared to runtime errors).

+6
source

Java does not support duck printing in a language. He maintains a reflection that can achieve the same. As far as I know, he has no relationship with Java Generics, because getting them to work together is a real pain.

+2
source

For me, “duck seal” means that there is no explicit correspondence relationship. If something goes like a duck and speaks like a duck, it can be seen as a duck, and it is not necessary to explicitly state that it is a duck. In C ++ terms, it does not need to inherit from the Duck base class: inheritance is a way to declare that one class explicitly matches the interface of another.

This concept is orthogonal to whether the check is checked at run time or at compile time. Languages ​​such as Smalltalk provide duck printing that occurs at run time (and inheritance is used to reuse the implementation, not to declare the interface to match). C ++ templates are a form of duck input that occurs at compile time.

And the last sentence is the answer to the question.

+1
source

Source: https://habr.com/ru/post/1434575/


All Articles