Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Even though Boo is a statically typed language, Duck Typing is a way to fake being a dynamic language. Duck typing allows variables to be recognized at runtime, instead of compile time. Though this can add a sense of simplicity, it does remove a large security barrier.

Code Block
borderStylesolid
titleDuck Typing Example
borderStylesolid
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
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
5
15
Hi there
HI THERE

Duck typing is very handy if you are loading from a factory or an unpredictable dynamic library.

...

Here is a practical example of where duck typing is useful.

Code Block
borderStylesolid
titlePractical Duck Typing
borderStylesolid
import System.Threading

def CreateInstance(progid):
    type = System.Type.GetTypeFromProgID(progid)
    return type()

ie as duck = CreateInstance("InternetExplorer.Application")
ie.Visible = true
ie.Navigate2("http://www.go-mono.com/monologue/")

while ie.Busy:
    Thread.Sleep(50ms)

document = ie.Document
print("$(document.title) is $(document.fileSize) bytes long.")

...