Boo
  1. Boo
  2. BOO-246

Implicit duck typing option

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.7
    • Component/s: Compiler
    • Labels:
      None
    • Number of attachments :
      4

      Description

      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).

      1. boo-246-booc.patch
        24 kB
        Arron Washington
      2. boo-246-compileparams.patch
        0.7 kB
        Arron Washington
      3. boo-246-type.patch
        66 kB
        Arron Washington
      4. duckbydefault-32405.patch
        3 kB
        Doug H

        Activity

        Hide
        Arron Washington added a comment -

        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.

        Show
        Arron Washington added a comment - 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.
        Hide
        Arron Washington added a comment -

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

        Show
        Arron Washington added a comment - For the record, my SVN is /that/ out of sync with the main repository. =D
        Hide
        Doug H added a comment -

        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()

        Show
        Doug H added a comment - 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()
        Hide
        Rodrigo B. de Oliveira added a comment -

        Patch applied. Thanks!

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

        Show
        Rodrigo B. de Oliveira added a comment - Patch applied. Thanks! BTW, I renamed DuckByDefault to Ducky to make it consistent with the command line option.
        Hide
        Bill Wood added a comment -

        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?

        Show
        Bill Wood added a comment - 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?
        Hide
        Doug H added a comment -

        " 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)]

        Show
        Doug H added a comment - " 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)]
        Hide
        Bill Wood added a comment -

        Doug, is Ducky turned on for Booxw too?

        Show
        Bill Wood added a comment - Doug, is Ducky turned on for Booxw too?

          People

          • Assignee:
            Rodrigo B. de Oliveira
            Reporter:
            Doug H
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: