newbie question: function overloading

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default newbie question: function overloading

    I need to define a method that performs differently when operated on objects
    of different type (overloading). Currently I use various if's to check for
    the type of the object as follows:

    def somefunction(a)

    if a.kind_of?(someType)
    expression1
    return
    end

    if a.kind_of(someOtherType)
    expression2
    return

    end


    i am wondering if there is a simpler way to implement that without the if's,
    in a way similar to C++ overloading. I mean I would like to define two
    functions with the same name that will somehow be aware of the type of
    their argument...

    sincerely,

    DG

    Dimitrios Galanakis Guest

  2. Similar Questions and Discussions

    1. Newbie Question: How to Move Div in Spry Grow function
      So, this is probably a question that will result in a facepalm to my own head, but how do make a div move to the top left corner of a container div...
    2. #39361 [NEW]: mbstring function overloading - local value ignored
      From: christoph at ziegenberg dot de Operating system: Win XP SP 2 PHP version: 5.2.0 PHP Bug Type: mbstring related Bug...
    3. Creating Function Newbie question
      Hi I have written a small bit of code for my web site that basically is anti hack code. $checking = ($HTTP_GET_VARS); if (ereg ("+",...
    4. Newbie Question: Can PHP communicate with a C function/program
      I have several library functions that have been created in 'C' which I would prefer not to have to re-write all of them to PHP. Is there a way to...
    5. [PHP] OO function overloading?
      Hi, This statement isn't entirely correct, overloading is possible with the overload extension. http://www.php.net/overload Regards, Greg...
  3. #2

    Default Re: newbie question: function overloading

    On Fri, 3 Oct 2003, Dimitrios Galanakis wrote:

    # I need to define a method that performs differently when operated on objects
    # of different type (overloading). Currently I use various if's to check for
    # the type of the object as follows:
    #

    There are really two answers to this:

    1) Ryan Pavlik has created a library that provides overloading capability
    like this. You could give it a try and it might feel right to your C++
    ways.
    2) I would lean more heavily toward this one: The Ruby Way to accomplish
    what you're trying to accomplish very likely isn't to check type in this
    way. I remember coming to Ruby from C++/Java and wondering why I couldn't
    do method overloading like this. My brain was wired into the static
    typing way of doing things and I even felt like overloading was almost
    a necessary part of OO. It didn't take long before I stopped feeling the
    need for this capability as I started reading existing code and
    understanding the Ruby idioms better.

    Maybe if you could post a more specific example/statement of what you're
    trying to accomplish, we could see about helping you with option #2.

    Chad


    Chad Fowler Guest

  4. #3

    Default Re: newbie question: function overloading

    I was expecting an answer like that... I wanted to ask then if operator
    overloading if exactly similar to function overloading.
    I would like for example to define my own complex class. In this canse
    the * operator will perform differencly when multiplying a real and a
    complex and when multiplying two complex numbers (this is almost what I
    need to do only the difference is that instead of a complex class I have
    some other class for which it makes sence to multiply objects of that
    class together and with real numbers). In other words how can I simplify
    the following code by removing the ifs?



    class complex
    def *(something)
    result=self.dup
    if something.kind_of?(Numeric)
    result.real*=something
    result.imag*=something
    return result
    end
    if something.kind_of(complex)
    result.real=result.real*something.real-result.imag*something.imag
    ... etc etc etc
    end

    end

    end




    Chad Fowler wrote:
    > On Fri, 3 Oct 2003, Dimitrios Galanakis wrote:
    >
    > # I need to define a method that performs differently when operated on objects
    > # of different type (overloading). Currently I use various if's to check for
    > # the type of the object as follows:
    > #
    >
    > There are really two answers to this:
    >
    > 1) Ryan Pavlik has created a library that provides overloading capability
    > like this. You could give it a try and it might feel right to your C++
    > ways.
    > 2) I would lean more heavily toward this one: The Ruby Way to accomplish
    > what you're trying to accomplish very likely isn't to check type in this
    > way. I remember coming to Ruby from C++/Java and wondering why I couldn't
    > do method overloading like this. My brain was wired into the static
    > typing way of doing things and I even felt like overloading was almost
    > a necessary part of OO. It didn't take long before I stopped feeling the
    > need for this capability as I started reading existing code and
    > understanding the Ruby idioms better.
    >
    > Maybe if you could post a more specific example/statement of what you're
    > trying to accomplish, we could see about helping you with option #2.
    >
    > Chad
    >
    >
    Dimitrios Galanakis Guest

  5. #4

    Default Re: newbie question: function overloading

    > From: Dimitrios Galanakis [mailto:galanaki@uiuc.edu]
    > I need to define a method that performs differently when
    > operated on objects of different type (overloading).
    > Currently I use various if's to check for the type of
    > the object as follows:
    >
    > def somefunction(a)
    > if a.kind_of?(someType)
    > expression1
    > return
    > end
    > if a.kind_of(someOtherType)
    > expression2
    > return
    > end
    > end
    I would do it like this ...

    class SomeType
    def somefunction
    expression1
    end
    end

    class SomeOtherType
    def somefunction
    expression2
    end
    end

    Then just call:

    a.somefunction

    If you still feel the need to pass obj as an argument to somefunction, you
    can always use:

    def somefunction(a)
    a.somefunction
    end

    --
    -- Jim Weirich / Compuware
    -- FWP Capture Services
    -- Phone: 859-386-8855

    Weirich, James Guest

  6. #5

    Default Re: newbie question: function overloading

    On Fri, 3 Oct 2003, Weirich, James wrote:

    # I would do it like this ...
    #
    # class SomeType
    # def somefunction
    # expression1
    # end
    # end
    #
    # class SomeOtherType
    # def somefunction
    # expression2
    # end
    # end
    #
    # Then just call:
    #
    # a.somefunction
    #
    # If you still feel the need to pass obj as an argument to somefunction, you
    # can always use:
    #
    # def somefunction(a)
    # a.somefunction
    # end
    #
    #

    And if you'd like to see that diagrammed:
    [url]http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html[/url]

    :)


    Chad Fowler Guest

  7. #6

    Default Re: newbie question: function overloading

    Dimitrios Galanakis wrote:
    > I was expecting an answer like that... I wanted to ask then if operator
    > overloading if exactly similar to function overloading.
    > I would like for example to define my own complex class. In this canse
    > the * operator will perform differencly when multiplying a real and a
    > complex and when multiplying two complex numbers (this is almost what I
    > need to do only the difference is that instead of a complex class I have
    > some other class for which it makes sence to multiply objects of that
    > class together and with real numbers). In other words how can I simplify
    > the following code by removing the ifs?
    >
    I'm still a relative Ruby newbie, so I can't bang out any example code,
    but I hope I can point out a couple of useful tidbits. In Ruby classes
    aren't closed. That means you can add methods to Numeric whenever you
    want. Also, as written, your code wouldn't work if you multiplied
    Numeric * Complex

    I'd make your new class a subclass of Numeric, and then modify the
    Numeric class. Alias the old * to something else, and put your own
    implementation in place. I still can't see how to get rid of an 'if',
    but your new method can check if either element is Complex then call a
    complex_multiply method, else use the aliased old *.

    -Greg Vaughn
    >
    >
    > class complex
    > def *(something)
    > result=self.dup
    > if something.kind_of?(Numeric)
    > result.real*=something
    > result.imag*=something
    > return result
    > end
    > if something.kind_of(complex)
    >
    > result.real=result.real*something.real-result.imag*something.imag
    > ... etc etc etc
    > end
    >
    > end
    >
    > end

    Greg Vaughn Guest

  8. #7

    Default Re: newbie question: function overloading

    Dimitrios Galanakis wrote:
    > I need to define a method that performs differently when operated on
    > objects of different type (overloading). Currently I use various
    > if's to check for the type of the object as follows:
    >
    > def somefunction(a)
    >
    > if a.kind_of?(someType)
    > expression1
    > return
    > end
    >
    > if a.kind_of(someOtherType)
    > expression2
    > return
    >
    > end
    >
    >
    > i am wondering if there is a simpler way to implement that without
    > the if's, in a way similar to C++ overloading. I mean I would like
    > to define two functions with the same name that will somehow be
    > aware of the type of their argument...
    >
    > sincerely,
    >
    > DG
    Check out the strongtyping module at RAA:

    [url]http://raa.ruby-lang.org/list.rhtml?name=strongtyping[/url]
    Oliver Dain Guest

  9. #8

    Default Re: newbie question: function overloading

    On Fri, 3 Oct 2003 06:14:45 +0900, Greg Vaughn wrote:
    > Dimitrios Galanakis wrote:
    >> I was expecting an answer like that... I wanted to ask then if operator
    >> overloading if exactly similar to function overloading. I would like
    >> for example to define my own complex class. In this canse the *
    >> operator will perform differencly when multiplying a real and a complex
    >> and when multiplying two complex numbers (this is almost what I need to
    >> do only the difference is that instead of a complex class I have some
    >> other class for which it makes sence to multiply objects of that class
    >> together and with real numbers). In other words how can I simplify the
    >> following code by removing the ifs?
    > I'm still a relative Ruby newbie, so I can't bang out any example code,
    > but I hope I can point out a couple of useful tidbits. In Ruby classes
    > aren't closed. That means you can add methods to Numeric whenever you
    > want. Also, as written, your code wouldn't work if you multiplied Numeric
    > * Complex
    In this case, what is wanted is Numeric#coerce.

    -austin
    --
    austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.10.02
    * 18.37.33



    Austin Ziegler Guest

  10. #9

    Default Re: newbie question: function overloading


    "Dimitrios Galanakis" wrote:

    ....
    > class together and with real numbers). In other words how can I simplify
    > the following code by removing the ifs?
    Short answer - you can't - the coerce frame-work is probably your best bet
    in a single dispatched world. Expect to see more and more `ifs and elses"
    the deeper you extend the Numeric class hierarchy. See

    [url]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/51566[/url]

    for an interesting snippet on Matz's thinking on the subject.


    /Christoph


    Christoph Guest

  11. #10

    Default Re: newbie question: function overloading

    On Thu, 2003-10-02 at 17:12, Chad Fowler wrote:
    > And if you'd like to see that diagrammed:
    > [url]http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html[/url]
    And in our particular instance, we have an even stronger reason to move
    to polymorphism because the condition was based on a type (rather than
    an enumeration in the diagram example).

    --
    -- Jim Weirich [email]jweirich@one.net[/email] [url]http://onestepback.org[/url]
    -----------------------------------------------------------------
    "Beware of bugs in the above code; I have only proved it correct,
    not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

    Jim Weirich Guest

  12. #11

    Default Re: newbie question: function overloading


    "Dimitrios Galanakis" <galanaki@uiuc.edu> schrieb im Newsbeitrag
    news:4i_eb.147$fm2.69960@vixen.cso.uiuc.edu...
    > I need to define a method that performs differently when operated on
    objects
    > of different type (overloading). Currently I use various if's to check
    for
    > the type of the object as follows:
    >
    > def somefunction(a)
    >
    > if a.kind_of?(someType)
    > expression1
    > return
    > end
    >
    > if a.kind_of(someOtherType)
    > expression2
    > return
    >
    > end
    >
    >
    > i am wondering if there is a simpler way to implement that without the
    if's,
    > in a way similar to C++ overloading. I mean I would like to define two
    > functions with the same name that will somehow be aware of the type of
    > their argument...
    You can find further info here:
    [url]http://www.rubygarden.org/ruby?MethodOverloading[/url]

    This might be of interest, too:
    [url]http://www.rubygarden.org/ruby?KeywordArguments[/url]

    Regards

    robert

    Robert Klemme Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139