What is the meaning of (Any) in Raku - specifically the ()?

前端 未结 3 678
孤独总比滥情好
孤独总比滥情好 2021-01-17 12:44

Here is an experiment with Raku:

> my $x
(Any)
> my $y=1
1
> my @a=[1, 2]
[1 2]
> my %h=a=>\'b\'
{a => b}
> say \"nil\" unless $x
nil
         


        
3条回答
  •  借酒劲吻你
    2021-01-17 13:17

    There is a very simple answer.

    Any is a class. Specifically it is the default base class for every other class.

    In Raku you can pass around a class the same way you can pass an instance.

    my $a = 1;
    my $b = $a.WHAT;
    
    say $b;
    # (Int)
    

    The thing is if you try and use the class as if it were an instance, bad things will happen.

    say $b + 4;
    # ERROR: … must be an object instance of type 'Int', not a type object of type 'Int'.
    

    When you use the REPL it automatically calls .gist and prints the result.

    .gist is meant for humans to be able to understand what the value is.

    So then why would it add parenthesis around the name of the class?

    It makes sense to me that it does that to tell you it isn't a Str or some other instance.

    say 'Str'; # say calls .gist
    # Str
    
    say 'abc'.WHAT;
    # (Str)
    
    say 'abc'.WHAT.^name;
    # Str
    
    say 'abc'.^name;
    # Str
    

    All but one of those is an instance of the Str class.
    (Guess which one.)


    Basically the parens tell you that it would be an error to try and use it as an instance.

提交回复
热议问题