Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105332 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 36334 invoked from network); 23 Apr 2019 00:47:31 -0000 Received: from unknown (HELO mail-it1-f174.google.com) (209.85.166.174) by pb1.pair.com with SMTP; 23 Apr 2019 00:47:31 -0000 Received: by mail-it1-f174.google.com with SMTP id a190so20168955ite.4 for ; Mon, 22 Apr 2019 14:47:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=UyFolwslB2BZnGhdlJhXHqzNGB7aNHhCTTz0Yp4+pmg=; b=FL7hLaWTRl3ojTOtMLe5FU/2mp1xD06K+woX2dsQsoxh52BAFImeor+rocvqk9FYIg F3htQ98TkUvYGZWxTpP0HyoCDzHf1sJknb7DcSgh6boIpVEBmY4CKpng5t8BwsHA6fKU Nb7uyctin3R4ozH3ItkWwgD1f2uOIYRU/2/iIvu8n5E9UXswMX3MOa6/gPGjGTPj9sJp P2Qmbo6ht8/gCqEe5zXhAe+MlluTMOnpJRWpye/Ctz7U8bgy+5vbz0wLOeb6NlFwXKIj Kcixf7S/e+Ye3ywD596+zTvvzY5yu4OPVSO/drtRZcPJaY6iemeXyNAdgboyKo2X9mcT sMZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=UyFolwslB2BZnGhdlJhXHqzNGB7aNHhCTTz0Yp4+pmg=; b=RQxI1tsXuzczh8K+5xiAvXW+ACG+fmuDyGkjRxW5xMECLTQU/krTXmBQKtoQUdQeRh b7tykbZEN/r3DVx+eGr/0OdYTKickLNSVCHa4epG0KSI1SF6npqURgHzbJI0rDLL+cNW eBhp4oiVP5GMKCWqqxVEam4EKsengXocveGM20Ra9WzWPl3hZQPBQqO5G5KRmqXahOVS G1SalJWpqdEP53Pj1h5ybWGFFmDtHJA6XtnsVAUhg/29VY9HnGzCpjSn287SjSyDL8Jy NKByMv36yV1EnNwILA+1ntcsNJGUhZZSiDqyTw4nZl3CoRTxG9Dvqlk5Gpc+b2cNSoAo mlfg== X-Gm-Message-State: APjAAAV1JSsY52gvy8pivY2PHb17FFA3JzshYuxKf2klF/sXx5d+X1Fr EsKd6zkwUPSgktrEYP87DAGxwKRHKBxLe6VfzGqv+Yd6 X-Google-Smtp-Source: APXvYqzKp95Sva0fiSq0bRiHQk60JQhxnoRyD/khyKpweJP7HfbWizxXTxIOEwoBJ+IUHRwm1tHV4SJ7R+9qgDdUWvQ= X-Received: by 2002:a24:4682:: with SMTP id j124mr311107itb.90.1555969664517; Mon, 22 Apr 2019 14:47:44 -0700 (PDT) MIME-Version: 1.0 Date: Mon, 22 Apr 2019 23:47:33 +0200 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000228e370587256ba5" Subject: Object Type Casting Reloaded From: benjamin.morel@gmail.com (Benjamin Morel) --000000000000228e370587256ba5 Content-Type: text/plain; charset="UTF-8" Hi internals, I'd like to revive an old discussion about object type casting. The idea would be to allow (ClassName) casting: $service = (EmailService) $diContainer->get('email.service'); The above code would throw a TypeError if the value is not an instance of the given class. I see the following advantages: - Type safety: we can be sure that the value is of the correct type or that we'll get an Error. This syntax allows to fail early if the variable happens to not be of the expected type, and avoids much more verbose checks; - Static analysis: IDEs and static code analysis tools can now understand the type of the variable, without having to resort to `@var` annotations. These combine into a third advantage: readability. Today's equivalent of the above one-liner could be: /** @var EmailService $service */ $service = $diContainer->get('email.service'); if (! $service instanceof EmailService) { throw new TypeError('Expected instance of EmailService, ...'); } Which is a lot of boilerplate code that could be easily avoided by introducing this new syntax. Before moving forward and working on a formal RFC, I'd like to hear your thoughts: what's your early feeling about this? Did I miss other discussions around this subject? Are there any technical issues that come to mind? Could this feature help the upcoming JIT compiler produce more efficient machine code by knowing the type of the variable at compile time? etc. Note: "casting" might not be the perfect name here as what we're really doing is a type check, but this reuses the type casting syntax and resembles Java's object casting. Thank you, Ben --000000000000228e370587256ba5--