Journal(2005) | Blog(2006) | RandomLink | WhoAmI | LiveBookmark | HomePage

<<Previous: multi sub Examples (6, Perl6) {...}  >>Next: let @Examples[8] = 'Perl6';

sub infix:<(7)> ($Perl6, @Examples)

Category: Perl6   Keywords: Operator overloading Perl6

Description

关于操作符重载的笔记。

Details

操作符只是带有特殊名字的子程序。我们这里对上一章 multi sub Examples (6, Perl6) {...} 里介绍的 multi sub 的例子用操作符重新写一次:
multi sub infix:<(+)> (Num $a, Num $b) { return $a + $b; }
multi sub infix:<(+)> (Str $a, Str $b) { return $a ~ $b; }
multi sub infix:<(+)> ($a, $b) { return $a ~ '-' ~ $b; }

say 1 (+) 2; # 3
say 'a' (+) 'b'; # ab
say 0 (+) 'a'; # 0-a
这里唯一需要介绍的是 infix 的意思。

(pre|in|post|circum)fix

操作符我们分为一元操作符和二元操作符,当然还有三元和 circumfix/圆周型。

You should know

What I said is a part of Perl6.

<<Previous: multi sub Examples (6, Perl6) {...}  >>Next: let @Examples[8] = 'Perl6';

Options: +Del.icio.us

Related items Created on 2005-05-24 02:16:42, Last modified on 2005-05-25 21:45:30
Copyright 2004-2005 All Rights Reserved. Powered by Eplanet && Catalyst 5.62.