Issue Details (XML | Word | Printable)

Key: BOO-246
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Rodrigo B. de Oliveira
Reporter: Doug H
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Boo

Implicit duck typing option

Created: 03/Feb/05 03:58 PM   Updated: 05/Apr/05 02:00 PM   Resolved: 26/Mar/05 05:35 PM
Return to search
Component/s: Compiler
Affects Version/s: None
Fix Version/s: 0.7

Time Tracking:
Not Specified

File Attachments: 1. Text File boo-246-booc.patch (24 kB)
2. Text File boo-246-compileparams.patch (0.7 kB)
3. Text File boo-246-type.patch (66 kB)
4. Text File duckbydefault-32405.patch (3 kB)



 Description  « Hide

This idea was mentioned in a few mailing list posts:
http://archive.boo.codehaus.org/user/messages/530
http://archive.boo.codehaus.org/user/messages/558
http://archive.boo.codehaus.org/user/messages/559
(Rodrigo's response is not in the archives)

When a type in boo cannot be inferred, it resolves to "object". For example, this code will not work because "ToUpper()" is a method of type string, not type object:

l = [1,"asdf",3]
for item in l: //item's type is just "object"
if item isa string:
print item.ToUpper()
else:
print item

Currently you can cast to a string, which is fastest:
print (item as string).ToUpper()

or you can cast the item to a "duck" type so that the method resolution doesn't happen until runtime (this is slower):

for item as duck in l:
item.ToUpper() //now works if the item is really a string

We can create a compiler option to implicitly treat unknown types as type "duck" instead of type "object", so no "as duck" is necessary. This might be helpful for lighter scripting tasks where speed is not so important.

For example to make the above code sample work, in src/Boo.Lang.Compiler/TypeSystem/TypeSystemServices.cs, change the end of the GetEnumeratorItemType method (at line 424) from this:
return ObjectType;
to this:
return DuckType;

To make implicit duck typing optional, however, we would need to add the option to the Boo compiler (or else implement it as a compiler service like Rodrigo has discussed for other future options).



Arron Washington added a comment - 21/Mar/05 05:54 PM

Three patches.

*Implement implict duck typing for all objects whose generic type is "object." This includes vague statements like "x as object" and when iterating through an Enumerable that returns an object.

*Implemeted booc.exe with a switch "-ducky" to toggle implicit duck typing behavior.

*Implemented "DuckByDefault" in CompilerParameters to make the two above patches work properly and nonintrusively.


Arron Washington added a comment - 21/Mar/05 05:58 PM

For the record, my SVN is /that/ out of sync with the main repository. =D


Doug H added a comment - 24/Mar/05 07:10 PM

This version of the patch also allows you to have duck-typed parameters by default, like in the test1 case below.

//none of these work without the implicit duck typing option:
def test1(s):
print s.ToUpper()

def test2():
li = ["one", "two", "three"]
for item in li:
print item.ToUpper()

def test3():
arr = (1, "two", 3.0)
print arr[1].ToUpper()
print arr[0] / arr[2]

print "test1:"
test1("a string")
print "test2:"
test2()
print "test3:"
test3()


Rodrigo B. de Oliveira added a comment - 26/Mar/05 05:35 PM

Patch applied. Thanks!

BTW, I renamed DuckByDefault to Ducky to make it consistent with the command line option.


Bill Wood added a comment - 05/Apr/05 10:19 AM

The -ducky flag would be useful for the interactive shells, perhaps even more so.

Also, would it make sense to have a "ducky" attribute to turn on duckiness for a block?


Doug H added a comment - 05/Apr/05 01:44 PM

" The -ducky flag would be useful for the interactive shells, perhaps even more so."

Yeah, Ducky is on by default for the interactive interpreter now. You can turn it off by typing "interpreter.Ducky = false"

I updated the docs for the interpreter and duck typing options here:
http://docs.codehaus.org/display/BOO/Interactive+Interpreter
http://docs.codehaus.org/display/BOO/Duck+Typing

"Also, would it make sense to have a "ducky" attribute to turn on duckiness for a block?"

Maybe that's something we can look into when we have a general "options" macro or attribute for specifying multiple options such as:
[assembly:Options(RawArrayIndexing:true, Debug:true, Ducky:true)]


Bill Wood added a comment - 05/Apr/05 02:00 PM

Doug, is Ducky turned on for Booxw too?