Skip to content

Ops itself should have a proxy type class #87

@Atry

Description

@Atry

Suppose we want to store heterogenous types in a Array, and then show them. For type-safety, we can store Show.Ops in the Array

Welcome to the Ammonite Repl 1.0.0
(Scala 2.12.2 Java 1.8.0_131)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ import cats.instances.all._ 
import cats.instances.all._
@ import cats.Show.ops._ 
import cats.Show.ops._
@ import cats.Show 
import cats.Show
@ val harray: Array[Show.AllOps[_]] = Array(toAllShowOps(42), toAllShowOps("foo")) 
harray: Array[Show.AllOps[_]] = Array(cats.Show$ops$$anon$4@e8ce5b1, cats.Show$ops$$anon$4@6101fd7d)
@ for (element <- harray) {
    println(element.show)
  } 
42
foo

However, this does not compile if the foreach body requires a Show type class.

@ def f[A: Show](a: A) = println(a.show)
defined function f
@ harray.foreach(f(_)) 
cmd5.sc:1: could not find implicit value for evidence parameter of type cats.Show[cats.Show.AllOps[_$1]]
val res5 = harray.foreach(f(_))
                           ^

I wonder if it is possible to automatically generate a cats.Show[cats.Show.AllOps[_]] by forwarding abstract method calls to the Ops for @typeclass annotated type classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions