Comments (4)
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.
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.
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.
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)
- is the fully qualified check necessary?
- refactor to compiler plugin HOT 10
- Some generated public members don't have explicit return types
- add a simulacrum.TypeClass marker trait HOT 4
- Deriving instances from implicits? HOT 1
- varargs does not works HOT 2
- publish for Scala 2.13.0-M5? HOT 6
- Compiler error when using type constraint
- Type mismatch when generating instances for `F[G[_]]`
- Type missmatch errors
- @op annotation causing compilation error on 2.13.1
- Linker errors when used with ScalaJS 1.1.1 HOT 1
- Compilation fails when updating from cats 2.2.0 to 2.3.0 HOT 9
- Ops itself should have a proxy type class HOT 1
- covariant functor typeclass ops HOT 1
- Limit visibility of implementation details
- `sbt doc` fails on `@typeclasses` methods with scaladocs HOT 3
- `Ops` method generation curiosities HOT 4
- `@specialized` isn't "forwarded" to ops
- Experiment with scalagen
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simulacrum.