svn: /phpdoc/zh/trunk/language/types/ iterable.xml

[email protected] (Bole Chen) Tue, 10 Nov 2020 12:49:03 +0000
Newsgroups php.doc.zh
Message-ID <[email protected]>
avenger                                  Tue, 10 Nov 2020 12:49:03 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=351350

Log:
sync to en.

Changed paths:
    A   phpdoc/zh/trunk/language/types/iterable.xml

Added: phpdoc/zh/trunk/language/types/iterable.xml
===================================================================
--- phpdoc/zh/trunk/language/types/iterable.xml	                        (rev 0)
+++ phpdoc/zh/trunk/language/types/iterable.xml	2020-11-10 12:49:03 UTC (rev 351350)
@@ -0,0 +1,178 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- $Revision$ -->
+<!-- EN-Revision: 342988 Maintainer: anonymous #138 Status: ready -->
+<!-- Reviewed: avenger Maintainer: avenger -->
+<sect1 xml:id="language.types.iterable">
+ <title>可迭代对象</title>
+
+ <para>
+  <type>Iterable</type>是 PHP 7.1 中引入的一个伪类型。它接受任何实现了
+  <classname>Traversable</classname> 接口的 &array; 或对象。这些类型都能用 &foreach; 迭代,并且可以与
+  <command>yield from</command> 在一个
+  <link linkend="language.generators">generator</link> 中使用。
+ </para>
+
+ <sect2 xml:id="language.types.iterable.using">
+  <title>使用可迭代对象</title>
+
+  <para>
+   可迭代对象可以用作参数类型,表示函数需要一组值,
+   但是不会关心值集的形式,因为它将与 &foreach; 一起使用。如果一个值不是数组或
+   <classname>Traversable</classname> 的实例,则会抛出一个
+   <classname>TypeError</classname>。
+  </para>
+
+  <para>
+   <example>
+    <title>
+     可迭代参数类型示例
+    </title>
+    <programlisting role="php">
+<![CDATA[
+<?php
+
+function foo(iterable $iterable) {
+    foreach ($iterable as $value) {
+        // ...
+    }
+}
+
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
+
+  <para>
+   声明为可迭代的参数可能会使用 &null; 或者一个数组作为默认值。
+  </para>
+
+  <para>
+   <example>
+    <title>
+     可迭代参数默认值示例
+    </title>
+    <programlisting role="php">
+<![CDATA[
+<?php
+
+function foo(iterable $iterable = []) {
+    // ...
+}
+
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
+
+  <para>
+   可迭代对象还可以用作返回类型,表示函数将返回一个可迭代的值。
+   如果返回值不是数组或 <classname>Traversable</classname>
+   的实例,则会抛出一个 <classname>TypeError</classname>。
+  </para>
+
+  <para>
+   <example>
+    <title>
+     可迭代返回类型示例
+    </title>
+    <programlisting role="php">
+<![CDATA[
+<?php
+
+function bar(): iterable {
+    return [1, 2, 3];
+}
+
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
+
+  <para>
+   将可迭代对象声明为返回类型的函数也可能是 <link
+   linkend="language.generators">generators</link>。
+  </para>
+
+  <para>
+   <example>
+    <title>
+     可迭代生成器返回类型的示例
+    </title>
+    <programlisting role="php">
+<![CDATA[
+<?php
+
+function gen(): iterable {
+    yield 1;
+    yield 2;
+    yield 3;
+}
+
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
+ </sect2>
+
+ <sect2 xml:id="language.types.iterable.variance">
+  <title>可迭代类型的类型差异</title>
+
+  <para>
+   扩展/实现的类可以把使用 &array; 或
+   <classname>Traversable</classname> 作为参数类型扩展为
+   <type>iterable</type> ,或者把“狭窄”的返回类型从 <type>iterable</type>
+   扩展为 &array; 或者 <classname>Traversable</classname>。
+  </para>
+
+  <para>
+   <example>
+    <title>
+     可迭代类型差异示例
+    </title>
+    <programlisting role="php">
+<![CDATA[
+<?php
+
+interface Example {
+    public function method(array $array): iterable;
+}
+
+class ExampleImplementation implements Example {
+    public function method(iterable $iterable): array {
+        // Parameter broadened and return type narrowed.
+    }
+}
+
+?>
+]]>
+    </programlisting>
+   </example>
+  </para>
+ </sect2>
+
+</sect1>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:1
+sgml-indent-data:t
+indent-tabs-mode:nil
+sgml-parent-document:nil
+sgml-default-dtd-file:"~/.phpdoc/manual.ced"
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+vim600: syn=xml fen fdm=syntax fdl=2 si
+vim: et tw=78 syn=sgml
+vi: ts=1 sw=1
+-->


Property changes on: phpdoc/zh/trunk/language/types/iterable.xml
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Id Rev Revision Date LastChangedDate LastChangedRevision Author LastChangedBy HeadURL URL
\ No newline at end of property