CSC/ECE 517 Fall 2007/wiki1 5 sl: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 5: Line 5:
''If it walks like a duck and quacks like a duck, I would call it a duck.''[1]
''If it walks like a duck and quacks like a duck, I would call it a duck.''[1]


In dynamically typed languages, such as Ruby, the parameters of a given method do not need to have a pre-specified type.  At run time, if the argument passed to the parameter has the necessary methods, the code will run.
In dynamically typed languages, such as Ruby, the parameters of a given method do not need to have a pre-specified type.  At run time, if the argument passed to the parameter has the necessary methods, the code will run. [2]


==Java Example==
==Java Example==
Line 73: Line 73:
The most obvious advantage of duck typing over static typing is conciseness of code. In a statically typed language, more code is often required to ensure type safety. Another major advantage is that duck typing is less restrictive since it allows the the end user to choose what type of object will be passed as parameters to any method at run time. As long as that argument has the required behavior, the method will function properly.
The most obvious advantage of duck typing over static typing is conciseness of code. In a statically typed language, more code is often required to ensure type safety. Another major advantage is that duck typing is less restrictive since it allows the the end user to choose what type of object will be passed as parameters to any method at run time. As long as that argument has the required behavior, the method will function properly.


The disadvantage of duck typing is the potential for errors to occur at run time when the program is out of the control of the programmer. In a statically typed language, type safety errors can be caught and corrected at compile time, before product release. As a result, statically typed languages are more suited for safety critical software, such as control systems.
The disadvantage of duck typing is the potential for errors to occur at run time when the program is out of the control of the programmer. In a statically typed language, type safety errors can be caught and corrected at compile time, before product release. As a result, statically typed languages are more suited for safety critical software, such as control systems. [3]


==See Also==
==See Also==
Line 80: Line 80:
For a real-world example of duck-typing in C#, visit http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx.   
For a real-world example of duck-typing in C#, visit http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx.   


Dr. Gehringer's notes on duck typing can be found at http://courses.ncsu.edu/csc517/common/lectures/notes/lec6.doc.
Dr. Ed Gehringer's notes on duck typing can be found at http://courses.ncsu.edu/csc517/common/lectures/notes/lec6.doc.


==References==
==References==
Line 87: Line 87:


[2] http://en.wikipedia.org/wiki/Duck_typing
[2] http://en.wikipedia.org/wiki/Duck_typing
[3] http://courses.ncsu.edu/csc517/common/lectures/notes/lec6.doc

Revision as of 19:03, 14 September 2007

Duck Typing

The term “duck typing” comes from the common American analogy of the duck test, which can be summed up with the following phrase:

If it walks like a duck and quacks like a duck, I would call it a duck.[1]

In dynamically typed languages, such as Ruby, the parameters of a given method do not need to have a pre-specified type. At run time, if the argument passed to the parameter has the necessary methods, the code will run. [2]

Java Example

Let us examine the following code snippet:

class ImaginaryNumber extends Object {
  public int realPart;
  public int imaginaryPart;

  public ImaginaryNumber(int realPart, int imaginaryPart) {
	  this.realPart = realPart;
	  this.imaginaryPart = imaginaryPart;
  }
  
  public int getRealPart() {
    return realPart;
  }

  public int getImaginaryPart() {
    return imaginaryPart;
  }
}

class MyMath {
  public static boolean equals(Object a, Object b) {   
    // this will create a compile-time error in Java
    return (a.getRealPart() == b.getRealPart());
  }
}

In a non-duck typed language, such as Java, the Equals() method above has two parameters a and b. It is required that these parameters have a specific type, in this case “Object”. A compile-time error will be generated even if two ImaginaryNumber objects are passed to the method. This occurs in spite of the fact that ImaginaryNumber has definitions for all of the methods called in Equals() and that ImaginaryNumber is a subclass of Object. In order to correct this error, Equals() would have to be changed to take two ImaginaryNumber objects as parameters. The corrected, functional version of the MyMath class is shown below.

class MyMath {
  public static boolean equals(ImaginaryNumber a, ImaginaryNumber b) {   
    return (a.getRealPart() == b.getRealPart());
  }
}

Ruby Example

In a duck typed language, such as Ruby, the Equals() method below would require parameters to have a defined type. A runtime error would be generated only if a non-ImaginaryNumber object was passed to the Equals() method. As long as only objects of type ImaginaryNumber are passed to the Equals() method, no error will ever be generated. The equivalent Ruby code is shown below.

class ImaginaryNumber
  def initialize(realPart, imaginaryPart)
    @realPart = realPart
    @imaginaryPart = imaginaryPart
  end
  attr_reader :realPart, :imaginaryPart
end 

class MyMath
  def self.equals(a, b)
    a.realPart == b.realPart
  end
end 
 

Duck Typing vs. Static Typing

The most obvious advantage of duck typing over static typing is conciseness of code. In a statically typed language, more code is often required to ensure type safety. Another major advantage is that duck typing is less restrictive since it allows the the end user to choose what type of object will be passed as parameters to any method at run time. As long as that argument has the required behavior, the method will function properly.

The disadvantage of duck typing is the potential for errors to occur at run time when the program is out of the control of the programmer. In a statically typed language, type safety errors can be caught and corrected at compile time, before product release. As a result, statically typed languages are more suited for safety critical software, such as control systems. [3]

See Also

For a more detailed example using Boo, a language that uses both static typing and duck typing, see http://boo.codehaus.org/Duck+Typing.

For a real-world example of duck-typing in C#, visit http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx.

Dr. Ed Gehringer's notes on duck typing can be found at http://courses.ncsu.edu/csc517/common/lectures/notes/lec6.doc.

References

[1] http://en.wikipedia.org/wiki/Duck_test

[2] http://en.wikipedia.org/wiki/Duck_typing

[3] http://courses.ncsu.edu/csc517/common/lectures/notes/lec6.doc