cvogt / compossible

Scala Records: Extensible records and type-indexed maps
https://www.youtube.com/watch?v=pA2js-TV1Ck
82 stars 10 forks source link

Destructuring a TMap in typeclasses #15

Open nightscape opened 8 years ago

nightscape commented 8 years ago

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 commented 8 years ago

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

cvogt commented 8 years ago

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

nightscape commented 8 years ago

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

cvogt commented 8 years ago

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.

Ciantic commented 8 years ago

@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.

nightscape commented 8 years ago

@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:

cvogt commented 8 years ago

@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.

nightscape commented 8 years ago

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.

cvogt commented 8 years ago

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