RFC: Type::Params용 새 API

14757 단어 perlrfc
첫째, 저는 Type::Params와의 호환성을 깨뜨릴 계획이 없습니다. 새 API는 Type::Params2와 같은 다른 네임스페이스 아래에 있습니다.

Type::Params용 API는 현재 다음과 같습니다.

use feature 'state';
use Type::Params qw( compile compile_named_oo );
use Types::Standard -types;

sub function_with_positional_parameters {
  state $check = compile( ArrayRef, Int, Int );
  my ( $list, $start, $end ) = $check->( @_ );

  my @slice = @{$list}[ $start .. $end ];
  return \@slice;
}

sub function_with_named_parameters {
  state $check = compile_named_oo( list => ArrayRef, start => Int, end => Int );
  my ( $arg ) = $check->( @_ );

  my @slice = @{$arg->list}[ $arg->start .. $arg->end ];
  return \@slice;
}


또는 다음이 있습니다.

use Type::Params qw( wrap_subs compile_named_oo );
use Types::Standard -types;

wrap_subs function_with_positional_parameters => [ ArrayRef, Int, Int ];

sub function_with_positional_parameters {
  my ( $list, $start, $end ) = @_;

  my @slice = @{$list}[ $start .. $end ];
  return \@slice;
}

wrap_subs function_with_named_parameters =>
  compile_named_oo( list => ArrayRef, start => Int, end => Int );

sub function_with_named_parameters {
  my ( $arg ) = @_;

  my @slice = @{$arg->list}[ $arg->start .. $arg->end ];
  return \@slice;
}


내가 제안하는 API는 다음과 같습니다.

use feature 'state';
use Type::Params2;
use Types::Standard -types;

sub function_with_positional_parameters {
  state $check = signature(
    pos => [ ArrayRef, Int, Int ],
  );
  my ( $list, $start, $end ) = $check->( @_ );

  my @slice = @{$list}[ $start .. $end ];
  return \@slice;
}

sub function_with_named_parameters {
  state $check = signature(
    named => [ list => ArrayRef, start => Int, end => Int ],
  );
  my ( $arg ) = $check->( @_ );

  my @slice = @{$arg->list}[ $arg->start .. $arg->end ];
  return \@slice;
}


인사이드 아웃 기술도 지원합니다.

use Type::Params2;
use Types::Standard -types;

signature_for function_with_positional_parameters => (
  pos => [ ArrayRef, Int, Int ],
);

sub function_with_positional_parameters {
  my ( $list, $start, $end ) = @_;

  my @slice = @{$list}[ $start .. $end ];
  return \@slice;
}

signature_for function_with_named_parameters => (
  named => [ list => ArrayRef, start => Int, end => Int ],
);

sub function_with_named_parameters {
  my ( $arg ) = @_;

  my @slice = @{$arg->list}[ $arg->start .. $arg->end ];
  return \@slice;
}


방법에 대한 바로 가기가 있습니다.

signature_for method_with_named_parameters => (
  method => 1,
  named  => [ list => ArrayRef, start => Int, end => Int ],
);

sub method_with_named_parameters {
  my ( $self, $arg ) = @_;

  my @slice = @{$arg->list}[ $arg->start .. $arg->end ];
  return \@slice;
}


코멘트? 사람들이 이것이 개선될 것이라고 생각합니까?

좋은 웹페이지 즐겨찾기