Coder Social home page Coder Social logo

Add `instance` support: about simulacrum HOT 4 OPEN

erikkaplun avatar erikkaplun commented on September 6, 2024 1
Add `instance` support:

from simulacrum.

Comments (4)

mpilquist avatar mpilquist commented on September 6, 2024

One thing to note with this syntax is that it wouldn't support multiple parameter lists designed to encourage type inference and syntax. For example, Monod.instance might be defined as:

def instance[A](id: A)(append: (A, A) => A): Monoid[A]

This definition works nicer with Scala's type inferencing rules. E.g., in Monoid.instance(0)(_ + _), A is correctly inferred as Int whereas in Monoid.instance(0, _ + _), Scala is not able to infer that the second param is a Function2[Int, Int, Int].

I don't think this is a blocker though -- folks can always define their own instance methods that are hand crafted to these types of situations. Just an observation.

from simulacrum.

erikkaplun avatar erikkaplun commented on September 6, 2024

To me, Monoid.instance[Int](...) is preferrable anyway rather than leaving off the type parameter. In Haskell you also declare it explicitly: instance Monoid Int where.

from simulacrum.

fommil avatar fommil commented on September 6, 2024

Found myself wanting something like this today. Makes sense when creating instances for typeclasses that have only one abstract method on them.

@typeclass trait UrlEncoded[T] {
  def urlEncoded(t: T): String
}

could helpfully generate

   def instance[T](body: T => String): UrlEncoded[T] = new UrlEncoded[T] {
     override def urlEncoded(t: T): String = body(t)
   }

on the companion allowing all the primitive impls to save on boilerplate. e.g.

implicit val urlEncodedString: UrlEncoded[String] = instance { s => URLEncoder.encode(s, "UTF-8") }

// @SystemFw

from simulacrum.

fommil avatar fommil commented on September 6, 2024

I'm wondering if there is something even more radical we could do. Imagine "first class instances" on the companion like this (for a typeclass F with a single abstract method that returns B)

def instance(f: Foo): B = ...

that would get converted into

implicit val FFoo: F[Foo] = instance(f => ... )

(or the longer form without instance).

I'm not sure if the @typeclass on the trait would allow for messing with the companion, or if the overloading of methods named instance would get as far as the macro stage... but it'd be lovely if it worked!

from simulacrum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.