Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8264515

Rename static factory methods in the foreign API

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • None
    • repo-panama
    • tools

      As a result of the recent API changes - specifically, the removal of so called “default” overloads - you can no longer say:

      ```
      MemorySegment.allocateNative(100); // scope missing here! What is the life cycle?
      ```

      The ResourceScope class offers an implicit scope factory, so this can become:

      ```
      MemorySegment.allocateNative(100, ResourceScope.ofImplicit()); // ok
      ```

      Being this relatively verbose, it’s not out of the realm of possibilities to think that users will just statically import ResourceScope, which leads to less clear code:

      ```
      MemorySegment.allocateNative(100, ofImplicit()); // ???
      ```

      In other words, the ofXYZ factories do not cope well with static imports. This problem is not just for ResourceScope - memory layouts have a similar issue:

      ```
      MemoryLayout layout = MemoryLayout.ofSequence(10,
                                        MemoryLayout.ofStruct(
                                                   MemoryLayout.ofValueBits(4).withName("x"),
                                                   MemoryLayout.ofPadding(8),
                                                   MemoryLayout.ofValueBits(4).withName("y"),
                                        ));
      ```

      That’s a lot of `MemoryLayout.` - but if we statically import:

      ```
      MemoryLayout layout =ofSequence(10,
                                        ofStruct(
                                                   ofValueBits(4).withName("x"),
                                                   ofPadding(8),
                                                   ofValueBits(4).withName("y"),
                                        ));
      ```

      The resulting code is not clear. We need some kind of uniform naming strategy and apply that consistently throughout the API.

            mcimadamore Maurizio Cimadamore
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: