+1 to everything Matt said.

On Sun, Nov 30, 2014 at 5:18 AM, Matt Gushee <[email protected]> wrote:

> Hi, Sean--
>
> I'm new to Julia myself, so take this with a grain of salt ... also,
> I'm not going to tell you that what you are trying to do is wrong or
> not done. However, I do think it is unidiomatic.
>
> I guess that, coming from an OO background, you are accustomed to
> thinking of 'method' as meaning a function that belongs to a
> particular object, and has access to that object's internal state. In
> a language like Julia that uses multiple dispatch, 'method' is a
> specialized implementation of a generic function. You probably read
> that somewhere, but if it's a new idea to you perhaps it's not clear
> what that means. There aren't many languages that fully support
> multiple dispatch, but it's very powerful. Anyway, I suppose it could
> be argued that 'methods' in Julia and in Java are in some sense the
> same thing - but I'll leave that argument to the academic computer
> scientists. There is no doubt that the two kinds of methods are very
> different in their surface manifestations.
>
> [PS: if you are indeed new to the concept of multiple dispatch, I've
> included a fun little exercise at the end that might give you some
> insights]
>
> On Sun, Nov 30, 2014 at 12:06 AM, Sean Gerrish <[email protected]>
> wrote:
>
> >   I'd find it natural to attach a method to a particular instance of an
> > object as in traditional object-oriented programming (see the example
> > below).  I can manage this with the constructor NewFoo, which binds the
> > instance to the method after the object is created.  Is this idiomatic in
> > Julia?  Is there a more idiomatic way?
>
> The question is, what problem are you trying to solve? You are trying
> to recreate a familiar programming paradigm in a language that doesn't
> natively support it. I would guess that as you become more familiar
> with this language, you'll discover a better solution. But that's hard
> to say without knowing what you are trying to accomplish in the real
> world.
>
> However, if we define the problem as "create a type that contains two
> numeric values, and a method that performs a computation with those
> values," here's my suggestion:
>
> type Foo
>      x::Number
>      y::Number
> end
>
> function compute(f::Foo)
>       f.x + f.y
> end
>
> foo = Foo(2, 3)
> compute(foo)  => 5
>
>
> And here's something you can try. I'm just showing code for you to
> enter - the results are omitted.
>
> julia> +
>
> julia> "a" + "b"
>
> julia> methods(+)
>
> julia> function +(a::String, b::String)
>               string(a, b)
>          end
>
> julia> +
>
> julia> "a" + "b"
>
> julia> methods(+)
>
> Pretty cool, huh? NOTE that I am not suggesting you distribute any
> code that does this for real - I think a lot of people would have a
> problem with extending '+' in that particular way; this is not Python.
>
> Hope that helps, and sorry for being long-winded.
>
> --
> Matt Gushee
>

Reply via email to