Message-ID: <1497187658.453.1369166204813.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_452_542869502.1369166204813" ------=_Part_452_542869502.1369166204813 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Boo is a static= ally typed language, like Java or C#. This means your boo applications will= run about as fast as those coded in other statically typed languages for .= NET or Mono. But using a statically typed language sometimes constrains you= to an inflexible and verbose coding style, with the sometimes necessary ty= pe declarations (like "x as int", but this is not often necessary= due to boo's Type Inference) a= nd sometimes necessary type casts (see Casting Types). Boo's support for Type Inference and eventually generics help here, but...
So= metimes it is appropriate to give up the safety net provided by static typi= ng. Maybe you just want to explore an API without worrying too much about m= ethod signatures or maybe you're creating code that talks to external compo= nents such as COM objects. Either way the choice should be yours not mine.<= /p>
Along with the normal types like object, int, string...boo has a spec= ial type called "duck". The term is inspired by the ruby programm= ing language's duck typing feature ("If it walks like a duck and quack= s like a duck, it must be a duck").
If you declare an object as = type duck or cast to type duck (or turn on the implicit duck typing option,= see below), then boo will not try to resolve any methods or operations you= call on that object at compile time. Instead it will convert those operati= ons into methods that do not resolve until runtime. So at compile time it s= imply trusts that you know what you are doing ("it walks like a duck s= o it must be a duck"). This is easier to understand by looking at some= examples.
This code illustrates some = things you can do with duck types that you can't with static types (unless = you add in casting, see Casting Type= s).
If it helps, whenever you see "duck", think "= any" or "anything". That object can be anything, and I can do whatever to it since it is duck typed and not static typed.<= h2 id=3D"DuckTyping-APracticalExampleAutomatingInternetExplorerviaCOMIntero= p">A Practical Example: Automating Internet Explorer via COM Interop
import S= ystem.Threading def CreateInstance(progid): type =3D System.Type.GetTypeFromProgID(progid) return type() ie as duck =3D CreateInstance("InternetExplorer.Application") ie.Visible =3D true ie.Navigate2("http://www.go-mono.com/monologue/") Thread.Sleep(50ms) while ie.Busy document =3D ie.Document print "$(document.title) is $(document.fileSize) bytes long."=09=09
See also how the example would look without duck typing.
You = can replace "ie as duck" in the internet explorer example with &q= uot;ie" and still have it run if you turn on the implicit duck typing = option. See also the examples in the tests/testcases/ducky/ folder.
You can set this option in variou= s ways depending on which tool you are using:
= What this option does is basically make anything that is of type "obje= ct" be of type "duck" instead.
Turning on the ducky op= tion allows you to more quickly test out code, and makes coding in boo feel= much more like coding in Python. However, it slows down the speed of your = application due to not using static typing. So when you are done testing, y= ou may wish to turn the ducky option back off and add in various type decla= rations and casts. See Casting Types= .
Regular Type Inference<= /a> is still in effect even when the ducky option is turned on, so if you d= eclare "x =3D 4", then x is still an int. This allows your code t= o still be convertible to run without the ducky option by adding in the app= ropriate type declarations and casts.
The = -ducky option now automatically treats any class that implements the IQuack= Fu interface as a duck type too.
Your compiled assembly will still be CLS compliant (usab= le from other languages) if you use duck types. "Duck" is only a = virtual type during the compile process. Duck types are converted into regu= lar, CLS-compliant "object" types in the assembly. Again, see wha= t the IE example looks like wi= thout duck typing to get an idea of how the compiler converts your code= . Also, IQuackFu is a regular interface. Not special at all except to the b= oo compiler.------=_Part_452_542869502.1369166204813--