Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Take a step closer to Signatures as constraints on variables. #6327

Open
p6rt opened this issue Jun 4, 2017 · 1 comment

Comments

@p6rt
Copy link

p6rt commented Jun 4, 2017

Migrated from rt.perl.org#131511 (status was 'new')

Searchable as RT131511$

@p6rt
Copy link
Author

p6rt commented Jun 4, 2017

From @BenGoldberg1

With the current version of rakudo, if I were to run the code “my &foo ();”,
I would get the error “Signatures as constraints on variables not yet
implemented.”

To bring us a little closer to being able to implement this, I propose the
following​:

First, provide a subtype (subrole?) of the Callable role, which has a
signature. These subrole would probably be created by writing
Callable[​:(...)].

Second this paramaterized version of Callable would need to be somewhere in
the ancestry of every existing Block, Routine, Sub, Method, Submethod and
Macro objects, or at least, for those which have a signature.

Third, make "my &varname (blah) ;" become nearly the same as "my
Callable[​:(blah)] $varname;" except of course for the sigil.

Fourth, ensure that paramaterization of Callable *doesn't* produce a
separate, distinct role for every signature passed in, but caches based on a
canonicalized version of the signature. For now, canonicalization of a
signature simply means removing the names of positional parameters, so :(Int
$foo) is transformed into :(Int $)

These four changes together mean that a user can write​:

constant two_ints_sub_t = Callable[​:(Int $asdf, Int $qwerty -->Int)];
my two_ints_sub_t $foo = sub add (Int $lhs, Int $rhs --> Int) { $lhs+$rhs };
my (Int $x, Int $y --> Int) &bar;
say $foo.VAR.WHAT; # "(Callable[​:(Int $, Int $-->Int)])"
say &bar.VAR.WHAT; # "(Callable[​:(Int $, Int $-->Int)])"
say $foo.VAR.WHAT === &bar.VAR.WHAT; # "True"

A fifth step, adding a method named sub_signature to the paramaterized role,
which returns the captured signature, would make it NativeCall friendly.

The drawback of this proposal is that it only works with signatures which
don't differ other than parameter names. But at least it's a start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant