Java N-Tuple implementation

后端 未结 9 2368
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 16:53

I just made a Java n-tuple which is type-safe.
I\'m using some unconventional methods to achieve type-safety (I just made it for fun).

Can someone can give some

相关标签:
9条回答
  • 2020-12-07 17:29

    You should look at .NET's Tuple's implementation. They are compile time type-safe.

    0 讨论(0)
  • 2020-12-07 17:30

    It would be better to use generics for compile time type safety. You can define one interface per arity. Then you can define separate Callable interfaces to access the values of the tuple.

    interface Tuple1 <T0> { <R> R accept ( Callable1<R,T0> callable ) ; }
    
    interface Tuple2 <T0,T1> { <R> R accept ( Callable2<R,T0,T1> callable ) ; }
    
    ...
    
    interface Tuplek <T0,T1,T2,...,Tk> { <R> R accept ( Callablek<R,T0,T1,T2,...,Tk> callable ) ; }
    
    interface Callable1<R,T0> { R call ( T0 t0 ) ; }
    
    interface Callable2<R,T0> { R call ( T0 t0 , T1 t1 ) ; }
    
    ....
    
    interface Callablek<R,T0,T1,T2,...,Tk> { R call ( T0 t0 , T1 t1 , T2 t2 , ... , Tk tk ) ; }
    
    0 讨论(0)
  • 2020-12-07 17:33

    What is the purpose of typeLock? To allow someone to prevent constructing any more of these objects? This part doesn't make much sense.

    Why would you ever want to let someone prevent further instantiation of your objects? If for some reason this is something you ever need, instead of "locking" a class and throwing exceptions, just make sure the code path ... doesn't create more objects of the type.

    What's the purpose of the static lastTuple which is set to a reference of the last instantiated Tuple? It's a poor practice to mix static references like this.

    Frankly the code is quite confusing, even though the need for this class is confusing. If somehow this was code I was reviewing in a work environment, I would not allow it.

    0 讨论(0)
  • 2020-12-07 17:36

    saw this code in wave project

    public class Tuple<A> {
    
      private final A[] elements;
    
      public static <A> Tuple<A> of(A ... elements) {
        return new Tuple<A>(elements);
      }
    
      public Tuple(A ... elements) {
        this.elements = elements;
      }
    
      public A get(int index) {
        return elements[index];
      }
    
      public int size() {
        return elements.length;
      }
    
      public boolean equals(Object o) {
        if (this == o) {
          return true;
        }
    
        if (o == null || o.getClass() != this.getClass()) {
          return false;
        }
    
        Tuple<A> o2 = (Tuple<A>) o;
        return Arrays.equals(elements, o2.elements);
      }
    
      @Override
      public int hashCode() {
        return Arrays.hashCode(elements);
      }
    
      @Override
      public String toString() {
        return Arrays.toString(elements);
      }
    }
    
    0 讨论(0)
  • 2020-12-07 17:42

    How is this typesafe? You are throwing runtime exceptions instead of reporting type errors at compile time.

    You are trying to abstract over arity which is (as of yet) not possible in statically typed languages, without losing typesafety.

    Addendum:

    Tuples can consist of heterogeneous elements (i.e. elements with different types). Therefore providing even "rutime typesafety" is not possible, for this Tuple class. Clients of the class are responsible for making the appropriate casts.

    This is the best you can do in Java : (Edit: See Brent's post for a better implementation of Tuple. (It doesn't require typecasts on the client side.))

    final class Tuple {
      private final List<Object> elements;
    
      public Tuple(final Object ... elements) {
        this.elements = Arrays.asList(elements);
      }
    
      @Override
      public String toString() {
        return elements.toString();
      }
    
      //
      // Override 'equals' and 'hashcode' here
      //
    
      public Object at(final int index) {
        return elements.get(index);
      }
    }
    
    0 讨论(0)
  • 2020-12-07 17:43

    This is the simplest solution and it's also the best. It's similar to how Tuples are represented in .NET. It carefully sidesteps java erasure. It is strongly typed. It does not throw exceptions. It is very easy to use.

    public interface Tuple
    {
        int size();
    }
    
    public class Tuple2<T1,T2> implements Tuple
    {
        public final T1 item1;
        public final T2 item2;
    
        public Tuple2(
            final T1 item_1,
            final T2 item_2)
        {
            item1 = item_1;
            item2 = item_2;
        }
    
        @Override
        public int size()
        {
            return 2;
        }
    }
    
    public class Tuple3<T1,T2,T3> implements Tuple
    {
        public final T1 item1;
        public final T2 item2;
        public final T3 item3;
    
        public Tuple3(
            final T1 item_1,
            final T2 item_2,
            final T3 item_3)
        {
            item1 = item_1;
            item2 = item_2;
            item3 = item_3;
        }
    
        @Override
        public int size()
        {
            return 3;
        }
    }
    
    0 讨论(0)
提交回复
热议问题