Results 1 to 3 of 3
  1. #1
    P3t3r is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Nested generics and ugly syntax

    While I know that generics will always be somewhat ugly in syntax, there are limits to what is feasible for programming. I wonder if the following could not be simplified, e.g. make part of it just copy its generics from another class.

    I'm doing mathematical simulations over certain (finite) fields, with different internal datatypes. So I work with generics: Field<ElementType,VectorType> can be
    • Field<Byte,byte[]> for small fields,
    • Field<Short,short[]> for large fields,
    • Field<Boolean,BitSet> for the binary field,
    etc. For matrices, I then need to use FieldMatrix<ElementType, VectorType, FieldType<ElementType,VectorType>>, hence declaring element and vector type twice.

    When considering stuctures over those fields, things seems to get arbitrary ugly. Projective points are just ProjectivePoint<ElementType, VectorType, Field<ElementType, VectorType>, projective lines are similarly ProjectiveLine<ElementType, VectorType, Field<ElementType, VectorType>, and the projective space is then ProjectiveSpace<ProjectivePoint<ElementType, VectorType, Field<ElementType, VectorType>, ProjectiveLine<ElementType, VectorType, Field<ElementType, VectorType>>, Field<ElementType, VectorType>, ElementType, VectorType>.

    And it gets even worse. Now I had to type ElementType and VectorType 6 times, but for embedding projective spaces in eachother, I have to type it 16 times. And it goes up.

    Am I doing this wrong, or do you see any way I could do this more efficient? I don't mind a long header, but I'd like to make it simple for people to contribute to my code. If GF implements Field<Byte,byte[]>, is there any way I can type new ProjectiveLine<GF>(parameters), rather than ProjectiveLine<Byte,byte[],GF>?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    i'm not sure I understand your problem 100%, but maybe this is helpful:

    Java Code:
    class Field<ElementType, VectorType> {
    //...
    }
    
    class GF extends Field<Byte, byte[]> {
    //...
    }
    
    class Test {
      GF gf = new GF(); // no need for parameters here at all
    }

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I guess if I were lazy and I were doing this, ....and I am in fact lazy, I'd make the generic key off only a single type. So instead of using Byte and byte[], I'd be lazy and just use Byte, and then use Byte[].

    Java Code:
    public class Junk<T extends Number> {
    
      public void stuff(T myT, T[] myTArray) {
          ...myT.doubleValue().....
          for(T thisT : myTArray) {
            ...thisT.doubleValue()....
          }
      }
    
    }

Similar Threads

  1. Generics
    By bschmitt78 in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2010, 02:21 AM
  2. Ugly XML to parse
    By JohnST in forum New To Java
    Replies: 2
    Last Post: 01-03-2010, 04:48 PM
  3. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 PM
  4. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:08 AM
  5. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •