3

So the problem is in title. Here r some exaplanations: As we know, we can use HList to store objects with its types.

SomeCL[Int], SomeCL[String], SomeCL[MyType1] :: ... :: HNil

Where SomeCL is some class. So I want to make make map, which can store information about all types inside, smth like:

// it is wrong usage of HMap, it is an example of how I want it be
HMap("string1" -> SomeCL[Int], "string2" -> SomeCL[MyType1], ... )

And not to loose information about types. I want to notice, that object types and number of objects r unknown, so we cant use lots of implicits for native shapeless HMap. Is it possible?

EDITED

(I meant that we have no possibility to write manually all implicit type convertions for shapeless HMap)

P.S.

If it is not possible, how can i decide such problem: For example I have some objects in HList, and I am building graph. It is very important to safe type of each node (we create nodes from objects in HList), and I dont know the number of objects. How can i build graph, and not to loose information about types?

EDITED2

Btw, this code will throw an error, if S is the super type of A and C; how can we avoid this?

class SomeCL[T](val l: T) {

}

class NodeMap[K, V]

implicit val si = new NodeMap[String, SomeCL[A]]
implicit val sd = new NodeMap[String, SomeCL[S]]
implicit val sb = new NodeMap[String, SomeCL[C]]

val hm = HMap[NodeMap]("foo" -> new SomeCL(new A), "bar" -> new SomeCL(new S), "lol" -> new SomeCL(new C))
DaunnC
  • 1,301
  • 15
  • 30
  • Any progress? It seems as I have the same questions. In my case `SomeCL[T]` is a little bit more complex (e.g. it has a HMap) and hence I'm not able to create the implicits manually :) Would be nice if the compiler could infer it. – user3127060 Feb 23 '16 at 12:19
  • @user3127060 can you ask a separate question or to provide more info? – DaunnC Mar 19 '16 at 22:15

2 Answers2

1

Yes, and it's provided for you in shapeless. See here.

emchristiansen
  • 3,550
  • 3
  • 26
  • 40
  • thx for reply, y, I tried to use implicits for shapeless `HMap`, but it throws error (try example in my post above, after `edited2`); I tried to make smth like : `val hmap = ("string1" ->> 3) :: ("string2" ->> true) :: ("string3" ->> boolean) :: HNil; hmap("string1");` mb it will help, I'll test it a little later, mb it suits me :) I'll write results. – DaunnC Nov 07 '13 at 07:37
0

that object types and number of objects r unknown

Then you cannot use compile time type information, obviously... You need to reconstruct types dynamically, using for example pattern matching

0__
  • 66,707
  • 21
  • 171
  • 266
  • mm, they r known ofcource before compile, but I meant that we have no possibility to write manually all implicit type convertions for shapeless `HMap` (for `BiMapIS`). btw u mb answered my question, im idiot, mb I should and I can write implicits. – DaunnC Nov 06 '13 at 21:14