tobyink / p5-zydeco

Perl 5 distribution Zydeco; see homepage for downloads and documentation.
https://zydeco.toby.ink/
14 stars 3 forks source link

Type aliases? #16

Open vendethiel opened 11 months ago

vendethiel commented 11 months ago

Hi! I recently found Zydeco and I've been toying with it recently.

I'm wondering if there's a way to declare a type alias, that has the same type as its underlying representation. Basically, something like type Level = NumRange[1, 80];. While investigating the libs behind Zydeco, I found out that use Type::Utils -all would give me a declare function (though it seems to be a keyword at other points, because I see declare MyType unquoted?), but I couldn't manage to get it to work doing it either from my main package or from my MAINPKG::Types (that I created originally to work around #15), they both error either when declaring it or when using it (sometimes in an unrelated file that also uses Zydeco...).

Thanks!

tobyink commented 4 months ago

Not pretty, but this works:

use v5.30;

BEGIN {
    package MyTest::Types;
    use Type::Library -base, -utils;
    use Types::Common qw( NumRange );
    declare Level => as NumRange[ 0, 80 ];
}

package MyTest {
    use Zydeco;

    class Bucket {
        has fill ( type => MyTest::Types::Level() );
    }
}

say MyTest->new_bucket( fill => 100 );

I agree that a nicer way to declare aliases would be good.

vendethiel commented 4 months ago

Ah, thanks. I was wondering if Exporter::Almighty had such a hatch, but this'll do for now. I also omit the () because otherwise,the type won't parse in method parameter type specifiers.