Comments (16)
To use the macro from external project, I think user must add
addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M10" cross CrossVersion.full)
to build.sbt, then external projet can call ShowMacro
from hamsters.
+1
from hamsters.
No need to use macro for that. We juste have to provide implicit for primitive, no ?
from hamsters.
How do you get the field names (name, age) without reflection?
The goal is to avoid to pass it explicitly
from hamsters.
If you want show filed names, I think use of macro is mandatory
from hamsters.
you don't want something like ? : https://github.com/typelevel/cats/blob/master/tests/src/test/scala/cats/tests/ShowTests.scala
Or you want the macro to do something like public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("Name", name).append("Age", age).toString(); }
Does it work with case class, class, sealed trait ?
from hamsters.
I was thinking of something very simple, to help debugging for example, with automatic generation like this : https://github.com/milessabin/kittens/blob/master/README.md#derive-show
But if a macro can generate automatically a default implicit, then I guess it would easy to pass a custom implicit too.
from hamsters.
Ok, i take the point
from hamsters.
Great!
I have a draft without the macro, I'll try to clean it/put it in a branch before tonight.
from hamsters.
Here is the branch : https://github.com/scala-hamsters/hamsters/tree/show
Edit : I've added a test case for deep case class formatting
from hamsters.
i made a pull request a few minutes ago.
it doesn't take case class with companion objects and it doesn't take other primitives than String and Int
from hamsters.
from hamsters.
For future improvements, do you think it would be possible to generate an implicit via the macro rather than annotating the case class?
Something like :
case class Person(name: String, age: Int)
implicit val personShowable = Show.format[Person]
val p = Person("bob", 40)
Show.show(p) // takes personShowable as implicit
For example it works this way with Play JSON :
implicit val personJsonFormat = Json.format[Person]
val jsonString = Json.toJson(p) // takes personJsonFormat as implicit
The main reason about this is that people usually don't want to annotate their case class, and I think the macro is not usable from the hamsters jar when it's compiled, am I wrong?
from hamsters.
It seems to be possible with Scala meta 2.0.0-M1. But we'll lose scala 2.11 support (see scalacenter/macros#1) .
Exemple : https://github.com/scalamacros/sbt-example-newstyle.
Maybe we could do something like :
import ShowableSyntax._
val p = Person("bob", 40)
println(p.show)
Which one do you prefer ?
I would like to give a try to def macros
from hamsters.
@dgouyette it seems to work in Play even with scala 2.10, is it a different use case?
About the syntax yes I like it, I think it would be nice to have Show.show and p.show (via syntax implicit class).
@oraclewalid macros are not included in the jar so it would not work.
from hamsters.
Remaining tasks for a first version of Show (using show branch) :
- make the macro usable from the tests (should work for any client of the lib too)
- move the case classes outside the Show.scala file (move to the right test)
- move show tests at the same level as other tests
from hamsters.
Related Issues (20)
- Hamsters REPL
- Macros not published for scala.js in the current build HOT 1
- Define a more explicit error message when implicit execution context is missing for monad transformers
- Add documentation for mapN
- Generate mapN for arity 4 to 22 using macros
- Test never ends if we add Monad instance for List HOT 1
- Write def (macro?) for Show HOT 2
- Enumerable.list should return a Set
- Rename Ko to Nok in Validation HOT 2
- Never ending test if we add monad list instance HOT 1
- Generate getSimpleName for case object
- Execution context should be passed implicitly in Retry
- Add a monad transformer for FutureTry HOT 4
- Macro compilation issues with Scala 2.13 HOT 6
- Move hlist and union types into extensions HOT 1
- Test new js package with mill
- Build in travis for Scala 2.12 and 2.13
- Integrate Validation.run in 4.0
- Make a 3.1 release with Long monoid
- FutureOps.fromOption HOT 1
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 hamsters.