TSa Thomas.Sandlass-at-barco.com |Perl 6| wrote:

  sub graph ( &func where {.does: Continuous}, Num $from, Num $to )
  {...}

  sub square ( Num $x --> Num ) does Continuous { return $x * $x }

  graph( &square, -10.0, 10.0 ); # type correct

The odd thing to me is that graph cannot be defined as

  sub graph ( Continuous &func, Num $from, Num $to ) {...}

or is that the same? Then how do I get what I want? I think S06 says
that the above means that &func returns a Continuous value as in

  sub foo ( Int &func:(Int) --> Int) { return func(17); }

  sub double (Int $x --> Int) { return 2 * $x; }

  say foo( &double ); # prints 34


In C++, the same problem:

void foo (Continuous (*f)()) // f is a function that takes nothing and returns Continuous
   void foo (Continuous *f)      // f is a Continuous

In Perl 6, within a signature

   sub foo (  &f:(Int-->Int) )
   sub foo (  Continuous &f )

The latter says that &f is of type Continuous, not that the return type is Continuous. That is what you want, right? The latter would be
   sub bar (  &f:(Int -->Continuous) )

A closure parameter block follows the block name. The normal annotation type before the name works in the usual manner, typing the whole object. It's not like the C++ syntax at all.

--John

Reply via email to