compossible icon indicating copy to clipboard operation
compossible copied to clipboard

Destructuring a TMap in typeclasses

Open nightscape opened this issue 8 years ago • 9 comments

Hi @cvogt,

I know you don't have much time for compossible atm, but maybe you could give me a hint on how one could destructure a TMap when using typeclasses. I've put up an example project and the problematic case (it's only a few lines) is here: https://github.com/nightscape/scala-destructure-tmap/blob/master/src/main/scala/TMapDeconstruction.scala I'm trying to extract multiple keys out of a TMap and processing them with type-classes and had hoped I could split up a TMap[Int with String] into an Int and a String in a generic way, but the compiler seems to split it up into a Int with String and something else.

I would be happy even if you could just throw me some terms to search for, I don't even know what the correct term for the Int with String part is...

Thanks a lot! Martin

nightscape avatar Jan 27 '16 22:01 nightscape

Ok, to answer my last question: The correct term seems to be intersection types.

nightscape avatar Jan 27 '16 22:01 nightscape

Well, I called them intersection types in my talk, but that is actually not the entire story, because sub-typing applies as well.

Looking at your code, I don't remember if I tried that, but I doubt the compiler would support something like this where you split up the type. You should be able to do that with (more) macros. Either directly, or by implementing a shapeless LabelledGeneric instance for TMaps as a macro, and then allow you to use shapeless' tooling to recurse over the keys and produce shows for them. There are macros that operate on the sets of keys here: https://github.com/cvogt/compossible/blob/tmp/0.2/src/main/scala/TMap.scala#L125

cvogt avatar Jan 27 '16 22:01 cvogt

Ok, I'll try my luck with macros then. Maybe it's even something that can be contributed back to compossible as PR.

nightscape avatar Jan 30 '16 20:01 nightscape

sure :)! I want to get back to compossible eventually myself. But my free time is currently occupied by the things I'll talk about at nescala and the typelevel summit.

cvogt avatar Jan 31 '16 18:01 cvogt

@nightscape did you also look at https://github.com/scala-records/scala-records/ if compossible is merged, like it may be https://github.com/scala-records/scala-records/issues/112 Scala could have a single good Record library.

I think having defacto Record library would make Scala amazing language, right now I think lack of Record library makes a difference in other frameworks. I know that developer of http://getquill.io/ wanted initially to implement own Record like DSL but thought it would be too much to learn, if Scala had records maybe it would be using it instead.

I really want to have a SQL library with a Record support in it's core, after all SQL is all about records.

Ciantic avatar Jan 31 '16 18:01 Ciantic

@Ciantic I had a look at scala-records and it looks very nice in fact. For my specific use case though a type-indexed map is the perfect fit :+1:

nightscape avatar Feb 03 '16 00:02 nightscape

@nightscape Another idea: You may be able to implement something like TMap on top of shapeless encoding the included types as an HList, which does allow element-wise extraction.

cvogt avatar Feb 03 '16 16:02 cvogt

I thought about that, too. But would that allow me to extract types in arbitrary order? For type class instances that require more than one parameter from the map I can't guarantee that they were added to the HList in the corresponding order.

nightscape avatar Feb 05 '16 16:02 nightscape

if shapeless hlists have some kind of contains operation, it should work. you'd need to ask for each of the parameters separately if they are contained

cvogt avatar Feb 05 '16 16:02 cvogt