Duck typing

Duck typing

En programmation informatique, le duck typing (traduction: typage canard) est un mode d'interprétation du code source utilisé par certains langages de programmation orientés objet à typage dynamique. En duck typing, la sémantique d'un objet, c'est-à-dire son type dans le contexte où il est utilisé, est déterminée par l'ensemble de ses méthodes et de ses attributs, et non, comme il est habituel, par un type défini explicitement par le programmeur.

L'analogie au canard vient de la phrase suivante: « Si je vois un animal qui vole comme un canard, cancane comme un canard, et nage comme un canard, alors j'appelle cet oiseau un canard ».

Ainsi, un objet d'une classe A donnée peut être considéré pour certaines opérations comme un objet d'une autre classe B, totalement différente (il n'est justement pas nécessaire qu'il y ait héritage entre les deux classes), pour certaines opérations, à condition que les méthodes et les attributs de B qui sont utilisés dans les opérations soient aussi présents dans A.

Exemple

fonction calcule(a, b, c) => renvoie (a+b)*c
 
a= calcule (1, 2, 3)
b= calcule ('pommes ', 'et oranges, ', 3)
 
affiche en_chaine a
affiche en_chaine b

Dans cet exemple, des objets sans relation d'héritage peuvent être utilisés (nombres, listes et chaines de caractères). Tant qu'ils gèrent les méthodes "+" et "*", l'opération fonctionne. Traduit dans des langages comme Ruby ou Python, la sortie serait :

 9
 "pommes et oranges, pommes et oranges, pommes et oranges, "

Wikimedia Foundation. 2010.

Contenu soumis à la licence CC-BY-SA. Source : Article Duck typing de Wikipédia en français (auteurs)

Игры ⚽ Нужна курсовая?

Regardez d'autres dictionnaires:

  • Duck Typing — ist ein Konzept der Objektorientierung, das die Anwendbarkeit bestimmter Verfahren nicht an die Ableitung der Klasse von einer bestimmten Superklasse oder der förmlichen Implementierung einer Spezifikation knüpft, sondern an das Vorhandensein… …   Deutsch Wikipedia

  • Duck-Typing — ist ein Konzept der objektorientierten Programmierung, bei dem der Typ eines Objektes nicht durch seine Klasse beschrieben wird, sondern durch das Vorhandensein bestimmter Methoden. Der Name ergibt sich in Anlehnung an James Whitcomb Rileys… …   Deutsch Wikipedia

  • Duck typing — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • duck typing — noun A style of dynamic typing in which an objects current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface …   Wiktionary

  • Duck (disambiguation) — Contents 1 Animals 2 Transportation 3 Computer software 4 …   Wikipedia

  • Duck Test — Sieht aus wie eine Ente, schwimmt wie eine Ente, quakt wie eine Ente: Das ist eine Ente Der Terminus Ententest (englisch duck test) beschreibt eine Methode einer analogen Begriffsbestimmung, wonach jemand versucht, die Natur eines bestimmten… …   Deutsch Wikipedia

  • Duck test — For the use of the duck test within the Wikipedia community, see Wikipedia:DUCK. A duck The duck test is a humorous term for a form of inductive reasoning. This is its usual expression …   Wikipedia

  • Dynamic typing — Dynamische Typisierung ist die Zuteilung des Typs einer Variablen zur Laufzeit eines Programms. Im Gegensatz zur statischen Typisierung verzichtet man auf eine explizite Typisierung; der Typ einer Variablen ergibt sich aus dem Typ des Werts, der… …   Deutsch Wikipedia

  • Latent typing — In computer programming, latent typing (as opposed to eager typing or manifest typing) is a style of typing that does not require (or perhaps even offer) explicit type declarations. Latent typing is heavily associated with duck typing and dynamic …   Wikipedia

  • Manifest typing — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”