CSC/ECE 517 Fall 2009/wiki1b 2 sk: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 38: Line 38:
          In type-inferred, the compiler infers the types of expressions and declarations based on the context.
          In type-inferred, the compiler infers the types of expressions and declarations based on the context.
<br>Many manifestly typed languages support partial type inference. For example, Java and C# both infer types in certain limited cases<br>
<br>Many manifestly typed languages support partial type inference. For example, Java and C# both infer types in certain limited cases<br>
<b> Combinations of Dynamic typing and Static typing </b>
The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, which is a form of dynamic typing.


= Advantages of Statically Typed v/s Dynamically typed =
= Advantages of Statically Typed v/s Dynamically typed =

Revision as of 06:42, 20 September 2009

Contents

1.Introduction

 1.1 Dynamically Typed Languages
 1.2 Statically Typed Languages
     1.2.1 Types of Statically Typed Languages
            1.2.1.1 Manifestly Typed 
            1.2.1.2 Type-inferred      

2. Combinations of Dynamic typing and Static typing
3. Advantages of Statically Typed v/s Dynamically typed
4. When Statically Typed is preferred over Dynamically typed
5. Examples
6. Conclusion
7. Glossary
8. References


Introduction

Dynamically Typed Languages
Dynamically typed languages are those in which type checking is done at run-time. In other words, the same entity does not always have the same form during the execution of the program and the type of the object assigned to a variable can be changed at runtime. Variable need not be defined before they are used. Some examples of dynamically typed languages are Ruby, Python, PHP, Smalltalk, Prolog, JavaScript, Objective-C.
/* Ruby code sample */
a = [1, 2, 3, 4, "Hi"]

The above code declares array that holds mix of integer and string types. Note that there was no need to specify array type and this would be evaluated automatically during runtime. However such a declaration is not possible in statically typed language like C, C++, Java, etc. doing so would give compile time errors.


Statically Typed Languages
Statically typed languages are those in which type checking is performed during compile-time as opposed to run-time. In static typing, all expressions have their types determined prior to the program being run (typically at compile-time). For example, 1 and (2+2) are integer expressions; they cannot be passed to a function that expects a string, or stored in a variable that is defined to hold dates. In other words, types are associated with variables and not values. This Static typing is a limited form of program verification. Accordingly, it allows many type errors to be caught early in the development cycle.
Some examples of Statically typed languages are Ada, C, C++, C#, JADE, Java, Pascal, Scala.


Types of Statically Typed Languages
Statically typed languages can be either manifestly typed or type-inferred.
          In manifestly typed, the programmer must explicitly write types at certain textual positions (for example, at variable declarations).
Most mainstream statically typed languages fall under the category of manifestly typed. Some of the examples are C++, C# , Java
          In type-inferred, the compiler infers the types of expressions and declarations based on the context.
Many manifestly typed languages support partial type inference. For example, Java and C# both infer types in certain limited cases


Combinations of Dynamic typing and Static typing The presence of static typing in a programming language does not necessarily imply the absence of all dynamic typing mechanisms. For example, Java, and various other object-oriented languages, while using static typing, require for certain operations (downcasting) the support of runtime type tests, which is a form of dynamic typing.


Advantages of Statically Typed v/s Dynamically typed

1.In static type languages, type checking is done at compile time so that helps catching type errors early in developlement life cycle.
2. Once the type of all variables have been checked, the process of type checking is not repeateadly perform over different runs of the same program. In contrast, dynamically typed langauge needs to perform type checking every time program is run.
3. Statically typed program runs faster as overhead of type checking during runtime is already performed during compile time.
4. Dynamic typed program may encounter runtime errors which are difficult to debug and tracking exact location of bug in the program may be difficult.