mermaid icon indicating copy to clipboard operation
mermaid copied to clipboard

Cannot have Generic of Generics in Class diagram

Open mohsenoid opened this issue 2 years ago • 3 comments

Describe the bug In the class diagram we can use ~ to achieve <T> generic types, for instance:

classDiagram
  MyInterface: +StateFlow~Int~

But we cannot have Generic of generic <T<K>>, for instnace:

classDiagram
  MyInterface: +StateFlow~List~Something~~

and the result is:

+StateFlow<List>Something<>

Related to #1120 and #1063

mohsenoid avatar Aug 04 '22 13:08 mohsenoid

Also related, these types of class diagrams with overloads of generic parameters do not render correctly either.

classDiagram

class A~T~ {
    +T value
}

class A~T,N~ {
    
}

class A~int~ {
    
}

A~int~ <|.. A~T~

Renders like:

classDiagram

class A~T~ {
    +T value
}

class A~T,N~ {
    
}

class A~int~ {
    
}

A~int~ <|.. A~T~

DeveloperPaul123 avatar Jan 26 '23 15:01 DeveloperPaul123

You can have List~Something~T~~ ids.

classDiagram
class A {
    List~Something~T~~ ids
}

So the issue is only with Class having generic types at the top. It might be a bug with the grammar.

sidharthv96 avatar Feb 19 '23 19:02 sidharthv96

The nested generics is good but this doesn't yet seem to support lists of generics as commonly used for things like Map<K,V>. Trying to diagram a class member that's a Map of string identifiers to a specific set of function types and the comma separated list of generics seems to give the current implementation a curve ball it didn't expect and it displays tilde's instead of the generics brackets.

Example:

class MyDemo<T> {
  private demoFns: Map<string, (s: T) => void>
}
classDiagram
  class MyDemo~T~ {
    #demoFns: Map~string, (s: T) => void~
  }
classDiagram
  class MyDemo~T~ {
    #demoFns: Map~string, (s: T) => void~
  }

BlueDreaux avatar Mar 14 '23 17:03 BlueDreaux

Example:

class MyDemo<T> {
  private demoFns: Map<string, (s: T) => void>
}
classDiagram
  class MyDemo~T~ {
    #demoFns: Map~string, (s: T) => void~
  }

So you mean specifically when you use a => statement, since the parser trips on the bracket after the equals?

jgreywolf avatar Apr 17 '23 01:04 jgreywolf

Apologies, I was thinking it was for the lack of generic list support but in testing with a fresh look it seems to handle the list of generics just fine.

Test:

class MyDemo<X,Y> {
  private xVector: X
  private yVector: Y
}
classDiagram
  class MyDemo~X,Y~ {
    #xVector: X
    #yVector: Y
  }

Seems to also work with default type specifications:

class MyDemo<X=Vector,Y=Vector> {
  private xVector: X
  private yVector: Y
}
classDiagram
  class MyDemo~X=Vector,Y=Vector~ {
    #xVector: X
    #yVector: Y
  }

However, in coming up with an example that avoids the js shorthand (thanks for pointing that out) I'm still seeing tilde's instead of brackets in what's generated.

class MyDemo<X,Y> {
  tests: Map<string,(X,Y): void>
}
classDiagram
  class MyDemo~X,Y~ {
    tests: Map~string,(X,Y): void~
  }

If I simplify the class members to mappings with a simple generic list I get a Maximum call stack size exceeded error:

class MyDemo<X=any,Y=any> {
  xTests: Map<string,X>
  yTests: Map<string,Y>
}
classDiagram
  class MyDemo~X,Y~ {
    xTests: Map~string,X~
    yTests: Map~string,Y~
  }

Furthermore, if I try to express an anonymous object type mapping I get a Diagram error not found. error I'm not sure how to begin unraveling.

class MyDemo<X,Y> {
  tests: Map<string,{X,Y}>
}
classDiagram
  class MyDemo~X,Y~ {
    tests: Map~string,{X,Y}~
  }

BlueDreaux avatar Apr 18 '23 15:04 BlueDreaux

The parser is choking on two aspects of this.

  1. The curly braces signal a "struct_start"/"struct_end" section, inside of which there should be the class definition with members.
  2. Commas inside of a generic value start/stop

This is going to take a little more time to find the best way to address this

jgreywolf avatar Apr 24 '23 15:04 jgreywolf