Hello,

I am already following Perl 6 since many years. But only recently as books began to appear, I began to really learn it.

One of the many things I like on Perl 6 is its static typing possibility. I am used to dynamical typed languages like Perl 5, Ruby, Python ... you name it. But I am very fond of strong static typing especially if it comes with automatic type inference like in languages like OCaml, Go and recently C++ since 2011. But I like languages like Ada too :-)

Now I would like to define two datatypes in Perl 6 deriving from `Int` but being incompatible with `Int` the same time.

For instance:

  `Distance` derived from `Int` with a range 0 up to 32000, and
  `Offset` derived from `Int` with a range from -32000 up to 32000

Now I would like the types `Distance`, `Offset` and `Int` being distinguishable and incompatible to each other on default.

So (pseudo Perl 6):

  my Distance $d = Distance(12);  // ok
  my Offset $o = Offset(-1);      // ok
  my Distance $d2 = $o;           // BUMMER!

  say $d + $o;                    // BUMMER!

and so on! I want the Perl 6 compiler to complain if I ever try to mix `Distance`s and `Offset`s implicitly.

In my books I've read so far there was no hint how to achieve this. Asking Google for some days also offer me no any answer whether this is possible, and if it is, how?

I found about `subset` but this only put some restriction on a type, but does not render it incompatible to the original type. Furthermore it is not distinguishable from the original type if its restrictions are met in both the original type and its subset.

So I would like to ask here if anybody know if this is possible in Perl 6? And if yes, how would I have to do it?

Thanks for your patience to follow me up to here ...


Best Regards,
chi.

Reply via email to