Re: Dylan vs Julia

Bruce Mitchener <[email protected]> Sun, 16 Feb 2014 09:50:28 +0700
Newsgroups gmane.comp.lang.dylan.gwydion.devel
Message-ID <CA+esKjM75sjC_AKHKfGxKYR6YqDWAhHvqi46Ztba5EzfMgf83A@mail.gmail.com>
On Sat, Feb 15, 2014 at 6:04 AM, mikel evins <[email protected]> wrote:

> Julia supports parametric polymorphism. Lifting an example from the Julia
> docs, you can define a type like this:
>
>   type Point{T}
>     x::T
>     y::T
>   end
>
>
> The type is called a parametric or parametrized type because part of its
> definition is a type parameter--in this case type T. Type T can be any type
> you like (but it has to be the same type in both x and y). That means that
> Point{T} is now actually a family of types like Point{Int64},
> Point{Float64}, and Point{String}. Julia functions can be specialized on
> any of these types, or on Point{T}.
>
> Dylan does not have this feature. On the other hand, Dylan does have
> limited types, which enable you to achieve many of the same effects. As an
> example, you can't express the equivalent in Dylan of the Julia
> Vector{Integer}, but you can define a limited type like this:
>

Using this same example from the Julia docs, i had posted this to the Julia
list on the same thread over there:

As for the difference between limited and parametric types, I can't do the
direct equivalent of using parametric types to define a Point class that
might have Float or Integer coordinates as in your documentation.  This is
sometimes annoying, but can also be worked around in some cases, like this
(I think ... not tested):

    define constant <point> = limited(<vector>, size: 2);
    define constant <integer-point> = limited(<vector>, of <integer>, size:
2);
    define constant <float-point> = limited(<vector>, of <single-float>,
size: 2);

    define inline method point-x (p :: <point>)
      p[0]
    end;

    define inline method point-y (p :: <point>)
      p[1]
    end;

    define inline method point-x-setter (x, p :: <point>)
      p[0] := x;
    end;

    define inline method point-y-setter (y, p :: <point>)
      p[0] := y;
    end;

    // This should work:
    let fp :: <float-point> = make(<float-point>);
    fp.point-x := 3;
    fp.point-y := 5;

This could be more elegant, but it should work as expected (so long as
Dylan treats <integer-point> and <float-point> as subtypes of <point>,
which I think it will).  The inlining there should get rid of any dispatch
in most cases and result in good code generation.

 - Bruce

_______________________________________________
hackers mailing list
[email protected]
https://lists.opendylan.org/mailman/listinfo/hackers