Dynamic typing

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 ihr zugewiesen wird. Erkauft wird dies mit gewissen Laufzeitnachteilen und erschwerter Fehlersuche. So ist es zum Beispiel notwendig, in Tests sämtliche Programmpfade zu durchlaufen, um Typisierungsfehler zu finden, die bei statischer Typisierung schon während der Übersetzung vom Compiler gefunden werden.

Vor allem Skriptsprachen wie z. B. JavaScript, Python und Ruby verwenden die dynamische Typisierung.

Inhaltsverzeichnis

Beispiele

Python

Hier eine interaktive Python-Sitzung:

>>> a = 1                         # a ist qua Zuweisung eine ganze Zahl
>>> a += 1.0                      # addiert die Gleitkommazahl 1.0 und ändert damit den Typ
>>> a.upper()                     # geht schief: a ist keine Zeichenkette
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'upper'
>>> a                             # gibt den Wert von a aus
2.0
>>> a = "jetzt ist a ein String"
>>> a += 1                        # geht schief: a ist jetzt ein String
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> a.upper()
'JETZT IST A EIN STRING'

Der Typ der Variablen a wird bei jeder Zuweisung aktualisiert. Die Fehlermeldung „… has no attribute 'upper'“ illustriert, dass der Python-Interpreter nicht unbedingt einen String verlangt, sondern mit einem beliebigen Objekt mit einer Methode upper zufrieden wäre (siehe Duck-Typing).

Boo

Beispiel von der Projektseite[1]

d as duck
d = 5 // currently set to an integer.
print d
d += 10 // It can do everything an integer does.
print d
d = "Hi there" // sets it to a string.
print d
d = d.ToUpper() // It can do everything a string does.
print d

Ausgabe:

5
15
Hi there
HI THERE

Erläuterung des Beispiels

Es wird die Variable d angelegt und ihr wird der Datentyp duck zugewiesen. Dies ist kein richtiger Datentyp, sondern nur eine Art Container, der andere Datentypen annehmen kann. In der zweiten Zeile wird d der Integerwert 5 zugewiesen.

In Zeile 6 wird ihr die Zeichenkette Hi there zugewiesen. In anderen Programmiersprachen wie beispielsweise C# oder C++ würde dies jetzt zu einem Compiler-Fehler führen.

Der Boo-Compiler hingegen erkennt am Datentyp duck, dass sich der Datentyp der Variable d ändern kann.

Siehe auch

Quellen

  1. Boo-Projektseite, Stand 19. April 2006, 19.52 Uhr

Wikimedia Foundation.

Игры ⚽ Поможем решить контрольную работу

Schlagen Sie auch in anderen Wörterbüchern nach:

  • dynamic typing — noun A system in which type checking is performed at run time and not at compilation, opposed to static typing …   Wiktionary

  • Dynamic programming language — This article is about a class of programming languages, for the method for reducing the runtime of algorithms, see Dynamic programming. Dynamic programming language is a term used broadly in computer science to describe a class of high level… …   Wikipedia

  • Dynamic SSL — is an endpoint security technology developed by Daniel McCann and Nima Sharifimehr of NetSecure Technologies Ltd. Dynamic SSL was created to solve the endpoint security problem in public networks by transparently correcting the implementation… …   Wikipedia

  • Dynamic logic (modal logic) — For the subject in digital electronics also known as clocked logic, see dynamic logic (digital electronics). Dynamic logic is an extension of modal logic originally intended for reasoning about computer programs and later applied to more general… …   Wikipedia

  • Dynamic dispatch — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping Vir …   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

  • 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

  • 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

  • Strong typing — Starke Typisierung (engl. strong typing, daher oft auch strenge Typisierung) bezeichnet ein Schema der Typisierung von Programmiersprachen. Bei der starken Typisierung bleibt eine einmal durchgeführte Bindung zwischen Variable und Datentyp… …   Deutsch 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

Share the article and excerpts

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