{"id":317768,"date":"2021-02-09T21:01:20","date_gmt":"2021-02-09T21:01:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=317768"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=317768","title":{"rendered":"Tagged Unions \u0432 PHP (\u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043a\u0430\u043a \u0432 Rust)"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p>\u0412 <a href=\"https:\/\/habr.com\/ru\/post\/541246\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u044f \u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 enums \u0432 PHP8.1. \u0413\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0448\u043b\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u043e\u043f\u0440\u043e\u0441 \u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0439.<\/p>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f enums \u2014 \u043b\u0438\u0448\u044c \u0447\u0430\u0441\u0442\u044c <a href=\"https:\/\/wiki.php.net\/rfc\/adts\" rel=\"nofollow noopener noreferrer\">\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u043d\u0430<\/a>. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0443\u043d\u043a\u0442, tagged unions, \u043f\u043e-\u0440\u0443\u0441\u0441\u043a\u0438 \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u0430\u043a &quot;\u0442\u0438\u043f-\u0441\u0443\u043c\u043c\u0430&quot;. <\/p>\n<p>  <\/p>\n<p><strong>\u0413\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e \u043d\u0435\u043c\u0443 \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043b\u043e<\/strong>, \u043d\u043e <a href=\"https:\/\/wiki.php.net\/rfc\/tagged_unions\" rel=\"nofollow noopener noreferrer\">\u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f<\/a> \u0442\u0430\u043a\u0436\u0435 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0432 PHP 8.1.<\/p>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u044d\u0442\u0438 \u0442\u0435\u0440\u043c\u0438\u043d\u044b &quot;\u0430\u043b\u0433\u0435\u0431\u0440\u0430\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445&quot;, &quot;\u0442\u0438\u043f-\u0441\u0443\u043c\u043c\u0430&quot; \u0437\u0432\u0443\u0447\u0430\u0442 \u0441\u0442\u0440\u0430\u0448\u043d\u043e, \u043d\u043e \u043d\u0430 \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u0447\u0435\u043c \u0432\u0441\u0451 \u044d\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0443\u0436\u043d\u043e?<\/p>\n<p>  <\/p>\n<h2 id=\"result-kak-v-raste\">Result \u043a\u0430\u043a \u0432 \u0420\u0430\u0441\u0442\u0435<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Rust, \u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 enum Result. \u0412 Rust, Go \u0438 \u0442.\u0434. \u043d\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 exception, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u044d\u0442\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044f\u0432\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043d\u0430\u0434\u0451\u0436\u043d\u0435\u0435. \u042f\u0437\u044b\u043a \u0432\u044b\u043d\u0443\u0436\u0434\u0430\u0435\u0442 \u0442\u0435\u0431\u044f \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0430 \u043d\u0435 \u043a\u0438\u0434\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 \u043d\u0430\u0434\u0435\u0436\u0434\u0435, \u0447\u0442\u043e \u043a\u0442\u043e-\u0442\u043e \u043d\u0430\u0432\u0435\u0440\u0445\u0443 \u0437\u043d\u0430\u0435\u0442 \u043e \u043d\u0451\u043c \u0438 \u0443\u043c\u0435\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c. (\u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0437\u0434\u0435\u0441\u044c \u0445\u043e\u043b\u0438\u0432\u0430\u0440\u0438\u0442\u044c, \u043d\u0430 \u0442\u0435\u043c\u0443 exceptions vs return type, \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u0432\u043e\u0451 \u043c\u043d\u0435\u043d\u0438\u0435). \u0415\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043f\u0440\u043e \u0420\u0430\u0441\u0442, \u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0440\u043e\u0434\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443, \u0447\u0430\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 Result.<\/p>\n<p>  <\/p>\n<p>Result \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 (case-\u043e\u0432 \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u043e\u043b\u043e\u0433\u0438\u0438 enum \u0432 PHP): Ok \u0438 Err. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 enum, \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442, \u043d\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0435\u0449\u0435 \u0438 \u0441\u0430\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041f\u0440\u0438\u0447\u0435\u043c, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0445\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0430, \u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0442\u0438\u043f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, integer (\u0441\u0442\u0430\u0442\u0443\u0441 HTTP-\u043e\u0442\u0432\u0435\u0442\u0430).<\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432 PHP, \u0435\u0441\u043b\u0438 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">enum Result {     case Ok(public string $json);     case Err(public int $httpStatus); }  function requestApi($url): Result {     \/\/ }<\/code><\/pre>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u044d\u0442\u043e\u0442 \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043a\u0443\u0434\u0430-\u0442\u043e \u0435\u0449\u0435, \u0438 \u0437\u043d\u0430\u043d\u0438\u044f \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 \u0438 \u0435\u0435 \u0442\u0438\u043f\u0435 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u0440\u043e\u043f\u0430\u0434\u0443\u0442.<\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u044f \u043f\u0438\u0441\u0430\u043b <a href=\"https:\/\/habr.com\/ru\/post\/541246\/\">\u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a>, enum \u2014 \u044d\u0442\u043e \u043f\u043e \u0441\u0443\u0442\u0438 \u043a\u043b\u0430\u0441\u0441, \u0432 \u043d\u0451\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u0442.\u0434. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0442\u0438\u043f-\u0441\u0443\u043c\u043c\u044b \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043a\u0430\u043a \u043e\u0431\u0449\u0438\u043c\u0438 \u043d\u0430 \u0432\u0435\u0441\u044c enum, \u0442\u0430\u043a \u0438 \u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 case.<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u043d\u0430\u0434\u044b Maybe (\u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 RFC):<\/p>\n<p>  <\/p>\n<h2 id=\"monada-maybe\">\u041c\u043e\u043d\u0430\u0434\u0430 Maybe<\/h2>\n<p>  <\/p>\n<p>(\u0412 \u0420\u0430\u0441\u0442\u0435 \u0442\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f Option)<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">enum Maybe {   \/\/ This is a Unit Case.   case None {     public function bind(callable $f)      {       return $this;     }   };    \/\/ This is a Tagged Case.   case Some(private mixed $value) {     \/\/ Note that the return type can be the Enum itself, thus restricting the return     \/\/ value to one of the enumerated types.     public function bind(callable $f): Maybe     {       \/\/ $f is supposed to return a Maybe itself.       return $f($this-&gt;value);     }   };    \/\/ This method is available on both None and Some.   public function value(): mixed {     if ($this instanceof None) {       throw new Exception();     }     return $this-&gt;val;   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435, \u0432 \u044d\u0442\u043e\u043c \u0435\u043d\u0430\u043c\u0435 \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430: Some \u0438 None, \u043f\u0440\u0438\u0447\u0435\u043c Some \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0430 None \u2014 \u043f\u0440\u043e\u0441\u0442\u043e None. \u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0432\u043e\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 bind. \u0418 \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 value()<\/p>\n<p>  <\/p>\n<p>\u0412 RFC \u043d\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u043e, \u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0436\u0435<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">$a = Maybe::Some(&quot;blabla&quot;); \/\/ \u0438\u043b\u0438 $a = Maybe::None $a-&gt;bind();<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0443\u0434\u0430-\u0441\u044e\u0434\u0430 Result \u0438\u043b\u0438 Maybe \u0441\u043c\u044b\u0441\u043b\u0430 \u043d\u0435\u0442, \u043d\u0443\u0436\u043d\u043e \u0433\u0434\u0435-\u0442\u043e \u0432\u0441\u0451 \u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u044d\u0442\u043e \u0434\u0435\u043b\u043e. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 pattern matching, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c RFC, \u043d\u043e \u043e\u043d \u0441\u044b\u0440\u043e\u0432\u0430\u0442\u044b\u0439. \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0444\u0430\u043d\u0442\u0430\u0437\u0438\u044e, \u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">$result = requestApi($url);  if ($result is Result::Some {%$json}) {     \/\/ \u0432\u044b\u0432\u043e\u0434\u0438\u043c $json }  if ($result is Result::Err {%$httpStatus}) {     \/\/ \u0432\u044b\u0432\u043e\u0434\u0438\u043c $httpStatus }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0438\u043b\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u043d\u043e \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c match.<\/p>\n<p>  <\/p>\n<h2 id=\"esche-primer\">\u0415\u0449\u0435 \u043f\u0440\u0438\u043c\u0435\u0440<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u0442\u044c \u043a\u0443\u0447\u0430 \u0432\u0435\u0449\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 tagged unions. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043a\u0442\u043e-\u0442\u043e \u043f\u0438\u0441\u0430\u043b \u043f\u0430\u0440\u0441\u0435\u0440, \u0442\u043e \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u0442\u0430\u043f\u043e\u043c \u0438\u0434\u0435\u0442 tokenizer (scanner), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 \u0442\u043e\u043a\u0435\u043d\u044b. \u0412\u043e\u0442 \u0442\u0443\u0442 \u043f\u0440\u044f\u043c\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043b\u043e\u0436\u0438\u0442\u0441\u044f: \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0438\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0432\u0438\u0434\u043e\u0432 \u0442\u043e\u043a\u0435\u043d\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 enum. \u041f\u0440\u0438\u0447\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u043e\u043a\u0435\u043d\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0439 \u043b\u0438\u0442\u0435\u0440\u0430\u043b \u0438\u043b\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440, \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0435\u0449\u0435 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0412 \u043a\u043e\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0451 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u0438 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e. \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">enum Token {    case Comma;    case LeftBrace;    case RightBrace;    case StringLiteral(public string $str);    case Identifier(public string $identifier);    \/\/ \u0438 \u0442.\u0434. }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"chto-dalshe\">\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h2>\n<p>  <\/p>\n<p>\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u043f\u0440\u0438\u043c\u0443\u0442 \u044d\u0442\u043e\u0442 RFC \u043d\u0430 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0438 \u0438\u043b\u0438 \u043d\u0435\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u044e\u0442, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442 \u044f\u0437\u044b\u043a \u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044f \u043d\u0430 \u0443\u0441\u0438\u043b\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430 \u043f\u0435\u0440\u0435\u043d\u044f\u0442\u0438\u0435 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 &quot;\u0444\u0438\u0448\u0435\u043a&quot; \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u043d\u0430\u043b\u0438\u0446\u043e. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438\u043c\u0443\u0442 tagged unions, \u0442\u043e \u044f \u0433\u043e\u0442\u043e\u0432 \u043f\u043e\u0441\u043f\u043e\u0440\u0438\u0442\u044c, \u043f\u0440\u0438\u043c\u0443\u0442 \u0438 pattern matching.<\/p>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0434\u0430\u043b\u044c\u0448\u0435 \u0441 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043c\u0430\u0442\u0447\u0438\u043d\u0433\u043e\u043c, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043d\u0430 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u043a\u0430\u043d\u0430\u043b <a href=\"https:\/\/t.me\/crossjoin\" rel=\"nofollow noopener noreferrer\">Cross Join<\/a>!<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/541670\/\"> https:\/\/habr.com\/ru\/post\/541670\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p>\u0412 <a href=\"https:\/\/habr.com\/ru\/post\/541246\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u044f \u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 enums \u0432 PHP8.1. \u0413\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0448\u043b\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u043e\u043f\u0440\u043e\u0441 \u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0439.<\/p>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f enums \u2014 \u043b\u0438\u0448\u044c \u0447\u0430\u0441\u0442\u044c <a href=\"https:\/\/wiki.php.net\/rfc\/adts\" rel=\"nofollow noopener noreferrer\">\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u043d\u0430<\/a>. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0443\u043d\u043a\u0442, tagged unions, \u043f\u043e-\u0440\u0443\u0441\u0441\u043a\u0438 \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u0430\u043a &quot;\u0442\u0438\u043f-\u0441\u0443\u043c\u043c\u0430&quot;. <\/p>\n<p>  <\/p>\n<p><strong>\u0413\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e \u043d\u0435\u043c\u0443 \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043b\u043e<\/strong>, \u043d\u043e <a href=\"https:\/\/wiki.php.net\/rfc\/tagged_unions\" rel=\"nofollow noopener noreferrer\">\u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f<\/a> \u0442\u0430\u043a\u0436\u0435 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0432 PHP 8.1.<\/p>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u044d\u0442\u0438 \u0442\u0435\u0440\u043c\u0438\u043d\u044b &quot;\u0430\u043b\u0433\u0435\u0431\u0440\u0430\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445&quot;, &quot;\u0442\u0438\u043f-\u0441\u0443\u043c\u043c\u0430&quot; \u0437\u0432\u0443\u0447\u0430\u0442 \u0441\u0442\u0440\u0430\u0448\u043d\u043e, \u043d\u043e \u043d\u0430 \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u0447\u0435\u043c \u0432\u0441\u0451 \u044d\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0443\u0436\u043d\u043e?<\/p>\n<p>  <\/p>\n<h2 id=\"result-kak-v-raste\">Result \u043a\u0430\u043a \u0432 \u0420\u0430\u0441\u0442\u0435<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Rust, \u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 enum Result. \u0412 Rust, Go \u0438 \u0442.\u0434. \u043d\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 exception, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u044d\u0442\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044f\u0432\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043d\u0430\u0434\u0451\u0436\u043d\u0435\u0435. \u042f\u0437\u044b\u043a \u0432\u044b\u043d\u0443\u0436\u0434\u0430\u0435\u0442 \u0442\u0435\u0431\u044f \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0430 \u043d\u0435 \u043a\u0438\u0434\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 \u043d\u0430\u0434\u0435\u0436\u0434\u0435, \u0447\u0442\u043e \u043a\u0442\u043e-\u0442\u043e \u043d\u0430\u0432\u0435\u0440\u0445\u0443 \u0437\u043d\u0430\u0435\u0442 \u043e \u043d\u0451\u043c \u0438 \u0443\u043c\u0435\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c. (\u041d\u0435 \u0431\u0443\u0434\u0435\u043c \u0437\u0434\u0435\u0441\u044c \u0445\u043e\u043b\u0438\u0432\u0430\u0440\u0438\u0442\u044c, \u043d\u0430 \u0442\u0435\u043c\u0443 exceptions vs return type, \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u0432\u043e\u0451 \u043c\u043d\u0435\u043d\u0438\u0435). \u0415\u0441\u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043f\u0440\u043e \u0420\u0430\u0441\u0442, \u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0440\u043e\u0434\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443, \u0447\u0430\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 Result.<\/p>\n<p>  <\/p>\n<p>Result \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 (case-\u043e\u0432 \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u043e\u043b\u043e\u0433\u0438\u0438 enum \u0432 PHP): Ok \u0438 Err. \u0412\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430 enum, \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442, \u043d\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0435\u0449\u0435 \u0438 \u0441\u0430\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041f\u0440\u0438\u0447\u0435\u043c, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0445\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0430, \u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0442\u0438\u043f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, integer (\u0441\u0442\u0430\u0442\u0443\u0441 HTTP-\u043e\u0442\u0432\u0435\u0442\u0430).<\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432 PHP, \u0435\u0441\u043b\u0438 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u043f\u0435\u0448\u043d\u044b\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">enum Result {     case Ok(public string $json);     case Err(public int $httpStatus); }  function requestApi($url): Result {     \/\/ }<\/code><\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-317768","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/317768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317768"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/317768\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}