scala - Runtime type checks / pattern matching involving variable arity types -


I am reviewing the problem of generic wrappers for a heterogeneous primary types group together. I am using type members, so the structure looks like this now:

  properties external [s] {type A1r inner inside: internal [s] {type a = a1 }} Specialty Inner [S] {type A def peer: A}   

Definitely the problem is definitely for specific items, for example:

 Diff Test [S] (o: External [s]): Option [External [S] {Type A1 = String}] = O Match {Case OS: External [S] {Type A1 = String} = & Gt; Some (OS) cases _ = & gt; None}   

This type does not work because of erasure. The problem is that I should be the essence of type parameter ereety for peer, i.e., (most) companions who also do a type of parameter [S] , but others do not, therefore, It is not possible to use a type constructor parameter for inner and / or external .

Cheap solutions require actual subclasses:

But I do not like this solution because I have many different partners, and I do not want to create dedicated wrapper classes for each of them. For example, there may be trouble in copying these items if I have to write the copy method in each of these subclasses.

So I have been left with the class tag? If I have the following two peer types with different types of parameters, then how will it be solved:

  attribute fu [s] type inner 1 [s] = inner [s] ] {Type A = Foo [S]} Type Inner 2 [S] = Inner [S] {Type A = String}   

?

If we forget about the matching pattern for a moment, then some plain old Reflections can be used to some extent:

  reflect the import. ClassTag Feature External [S] {At A1DR Inner: Inner [S] {Type A = A1} Def [A] (Unreal Tags: ClassTag [A]): ​​Options [External [S] {Type A1 = A }] = Inner .Pier Match {Case _: A => Some (this.estensionoff [outer [s] {type A1 = a}]) case _ = & gt; None}} Specialty Inner [S] {Type A def peer: A}   

Test:

  attribute Foo [S] val x = new external [unit] {type A1 = string value inside = new inner [unit] {type A = string val peer = "afu"}} val y = new external [unit] {type A1 = Foo [unit] well inside = new Inner [unit] {type A = Foo [unit] val peer = new Foo [unit] {}}} val xs = x.as [string] val xi = x.as [foo [unit]] val ys = y as [string] val yi = y.as [foo [unit]]   

The only problem is that high-type types The is not tested is:

  y .as [fu [not]] / something!   

The second idea is that my code is always required to present the S parameter. Then

  attribute Sys [S & lt;: Sys [S]] attribute inner [S & lt;: Sys [S], Elem [~ & lt;: Sys [~]]] {def Peer: Alem [S] def [A [~ lt;: sis [~]]] (false tag: class tag [a [s]]: option [internal [s, a]] = if (tag. ) .isDefined) Some (this Instance of [Inner [S, A]] and None} Type [S & LT;: SIS [S]] = Inner [S, AC] Feet Fu [S & LT;: SIS [S] [S & lt;: Sys [S]] Specialty I Sys [I] val I: I [I] In: New internal [I, Foo] {val pear = new Foo] I] {}} Val : These [i] = new inner [i, bar] {well peer = new b IAR [I] {}} emphasizes (i.as [fu] .design) emphasizes (i.as [bar] .isEmpty) emphasize (j.as [foo] .isEmpty) emphasize (j. As [bar] .isDefined)   

or the previous version has been changed to use any type:

  attribute internal [S & lt;: Sys [S]] {Type Elem def peer: Aleem Def [a [~ ~: Sys [~]]] (built-in tag: class tag [a [s]]: option [int [s] , A]] = if (tag. Unopili (peer). Design [Something]: [[Sys [S], A [~ & lt;: Sys [~]]] = Inner [S] [S & lt; {Type Elem = A [S]} Val I: Inner [I] = New Inner [I] {Type Elem = Foo [I]; Val colleague = new Foo [i] {}} Val j: Inner [i] = New Inner [I] {Type Elem = Bar [I]; Val Peer = New Bar [I] {}} puts emphasis (i.as [Foo] .isDefined) (i.as [bar] .Iiactistry ) Is emphasizing (js [fu] .iIactistry) bar] .isDefined) Val ix: InnerT [i, foo] = i.as [fu] .Get ix.peer.b Az   

... the underlying resolution can be beneficial for such that as Serializer [Inner [s]] which can be broken easily When version type parameters are included.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -