rfc:constants_in_traits

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:constants_in_traits [2022/07/01 22:46] – add future scope sjirfc:constants_in_traits [2022/08/04 19:17] (current) – Move the status to implemented sji
Line 1: Line 1:
 ====== PHP RFC: Constants in Traits ====== ====== PHP RFC: Constants in Traits ======
-  * Version: 0.1+  * Version: 0.2.1
   * Date: 2022-06-21   * Date: 2022-06-21
   * Author: Shinji Igarashi<sji@sj-i.dev>, Stephen Reay<stephen@koalephant.com>   * Author: Shinji Igarashi<sji@sj-i.dev>, Stephen Reay<stephen@koalephant.com>
-  * Status: Under Discussion+  * Status: Implemented
   * Implementation: https://github.com/php/php-src/pull/8888   * Implementation: https://github.com/php/php-src/pull/8888
   * First Published at: http://wiki.php.net/rfc/constants_in_traits   * First Published at: http://wiki.php.net/rfc/constants_in_traits
Line 40: Line 40:
  
 <code php> <code php>
-intertface FooInterface {+interface FooInterface {
     public const FLAG_1 = 1;     public const FLAG_1 = 1;
     public function doFoo(int $flags): void;     public function doFoo(int $flags): void;
Line 66: Line 66:
 trait Foo { trait Foo {
     public const FLAG_1 = 1;     public const FLAG_1 = 1;
-    public const FLAG_2 = 2;+    protected const FLAG_2 = 2; 
 +    private const FLAG_3 = 2;
  
     public function doFoo(int $flags): void {     public function doFoo(int $flags): void {
Line 74: Line 75:
         if ($flags & self::FLAG_2) {         if ($flags & self::FLAG_2) {
             echo 'Got flag 2';             echo 'Got flag 2';
 +        }
 +        if ($flags & self::FLAG_3) {
 +            echo 'Got flag 3';
         }         }
     }     }
Line 218: Line 222:
         CONSTANT as private;         CONSTANT as private;
     }     }
 +}
 +</code>
 +
 +==== Can be used in Enum ====
 +Enumerations can use traits having constants, which behave as if the constants were defined within the enumeration.
 +
 +<code php>
 +trait T {
 +    private const CONSTANT = 42;
 +}
 +
 +// OK
 +enum E: int {
 +    use T;
 +
 +    case CaseA = self::CONSTANT;
 } }
 </code> </code>
Line 327: Line 347:
  
 ==== Rust ==== ==== Rust ====
-Rust has associated constants. Asociated constants are constants associated with a type. If multiple definitions of the same name conflict, they must be disambiguated on reference using qualified paths [[https://doc.rust-lang.org/reference/items/associated-items.html#associated-constants|[17]]][[https://doc.rust-lang.org/reference/paths.html#qualified-paths|[18]]].+Rust has associated constants. Associated constants are constants associated with a type. If multiple definitions of the same name conflict, they must be disambiguated on reference using qualified paths [[https://doc.rust-lang.org/reference/items/associated-items.html#associated-constants|[17]]][[https://doc.rust-lang.org/reference/paths.html#qualified-paths|[18]]].
  
 ==== C++ ==== ==== C++ ====
Line 336: Line 356:
  
  
-===== Proposed Voting Choices ===== +===== Vote ===== 
-A 2/3 majority is needed for this RFC to pass. Voting will start on 5. July 2022 and end on 19. July 2022+A 2/3 majority is needed for this RFC to pass. Voting started on 5. July 2022 and end on 19. July 2022 
 + 
 +<doodle title="Allow constants in traits as proposed?" auth="sji" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
  
Line 347: Line 372:
 Stateful Traits [[https://link.springer.com/chapter/10.1007/978-3-540-71836-9_4|[22]]] default to trait state as trait local, but allow the programmer to selectively use merge behavior as needed. On the contrary, since PHP defaults to merge behavior, there may be a future extension that allows trait local to be explicitly declared. This option has even been mentioned in the old discussions, but it has not caught the attention of many people and has been on hold for more than a decade [[https://externals.io/message/35800|[11]]]. Perhaps it is time to reconsider this also. Stateful Traits [[https://link.springer.com/chapter/10.1007/978-3-540-71836-9_4|[22]]] default to trait state as trait local, but allow the programmer to selectively use merge behavior as needed. On the contrary, since PHP defaults to merge behavior, there may be a future extension that allows trait local to be explicitly declared. This option has even been mentioned in the old discussions, but it has not caught the attention of many people and has been on hold for more than a decade [[https://externals.io/message/35800|[11]]]. Perhaps it is time to reconsider this also.
  
 +==== Requiring specific interfaces or classes on traits ====
 +Hack can require that the composing class of a trait be a derived class of a specific class or an implementation of a specific interface [[https://docs.hhvm.com/hack/traits-and-interfaces/trait-and-interface-requirements|[23]]].
 +
 +At the same time, Hack also allows to implement interfaces from traits. That is, if a class C uses a trait T that implements an interface I, then C is a subtype of I [[https://github.com/facebook/hhvm/commit/6a2c5150edf4c6d3d1e015d665d85221b0975f45|[24]]][[https://github.com/facebook/hhvm/commit/6dac173dd5cc56c67303f7e6a46917f0aebad773|[25]]]. Actually, trait constants in Hack were introduced as a syntactic sugar for traits that implement interfaces with constants [[https://github.com/facebook/hhvm/commit/9dcc664cd19d71c849be8ce052b11175d275c331|[26]]]. 
 +
 +These can be other ways to tie properties or constants to traits, and this RFC does not preclude future implementation of any of them.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 374: Line 405:
   * [[https://www.scala-lang.org/files/archive/spec/2.13/05-classes-and-objects.html|[21]]] Scala Language Specification Version 2.13 | Classes and Objects   * [[https://www.scala-lang.org/files/archive/spec/2.13/05-classes-and-objects.html|[21]]] Scala Language Specification Version 2.13 | Classes and Objects
   * [[https://link.springer.com/chapter/10.1007/978-3-540-71836-9_4|[22]]] Stateful Traits   * [[https://link.springer.com/chapter/10.1007/978-3-540-71836-9_4|[22]]] Stateful Traits
 +  * [[https://docs.hhvm.com/hack/traits-and-interfaces/trait-and-interface-requirements|[23]]] HHVM and Hack Documentation | Traits And Interfaces: Trait And Interface Requirements
 +  * [[https://github.com/facebook/hhvm/commit/6a2c5150edf4c6d3d1e015d665d85221b0975f45|[24]]] facebook/hhvm | Allow traits to implement interfaces
 +  * [[https://github.com/facebook/hhvm/commit/6dac173dd5cc56c67303f7e6a46917f0aebad773|[25]]] facebook/hhvm | Allow traits to implement interfaces Add runtime support for implementing interfaces from traits
 +  * [[https://github.com/facebook/hhvm/commit/9dcc664cd19d71c849be8ce052b11175d275c331|[26]]] facebook/hhvm | Allow constants in traits
   * https://externals.io/message/110741 The initial discussion about constants in traits   * https://externals.io/message/110741 The initial discussion about constants in traits
  
rfc/constants_in_traits.1656715591.txt.gz · Last modified: 2022/07/01 22:46 by sji