GithubHelp home page GithubHelp logo

Comments (16)

oraclewalid avatar oraclewalid commented on July 27, 2024 2

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.

dgouyette avatar dgouyette commented on July 27, 2024

+1

from hamsters.

dgouyette avatar dgouyette commented on July 27, 2024

No need to use macro for that. We juste have to provide implicit for primitive, no ?

from hamsters.

loicdescotte avatar loicdescotte commented on July 27, 2024

How do you get the field names (name, age) without reflection?
The goal is to avoid to pass it explicitly

from hamsters.

oraclewalid avatar oraclewalid commented on July 27, 2024

If you want show filed names, I think use of macro is mandatory

from hamsters.

dgouyette avatar dgouyette commented on July 27, 2024

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.

loicdescotte avatar loicdescotte commented on July 27, 2024

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.

dgouyette avatar dgouyette commented on July 27, 2024

Ok, i take the point

from hamsters.

loicdescotte avatar loicdescotte commented on July 27, 2024

Great!
I have a draft without the macro, I'll try to clean it/put it in a branch before tonight.

from hamsters.

loicdescotte avatar loicdescotte commented on July 27, 2024

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.

dgouyette avatar dgouyette commented on July 27, 2024

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.

loicdescotte avatar loicdescotte commented on July 27, 2024

from hamsters.

loicdescotte avatar loicdescotte commented on July 27, 2024

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.

dgouyette avatar dgouyette commented on July 27, 2024

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.

loicdescotte avatar loicdescotte commented on July 27, 2024

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

loicdescotte avatar loicdescotte commented on July 27, 2024

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)

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.