GithubHelp home page GithubHelp logo

tarao / record4s Goto Github PK

View Code? Open in Web Editor NEW
63.0 4.0 3.0 843 KB

Extensible records for Scala

Home Page: https://tarao.orezdnu.org/record4s/

License: Apache License 2.0

Scala 98.67% Shell 0.72% Python 0.57% Dockerfile 0.04%
data-types records scala scala3

record4s's Introduction

record4s: extensible records for Scala

Build status Coverage status Maven Central Scaladoc
record4s Scala version support record4s Scala version support record4s Scala version support

record4s proviedes extensible records for Scala.

See the guide to learn about installation and usage.

License

record4s is licensed under Apache License, Version 2.0.

record4s's People

Contributors

chencmd avatar quafadas avatar renovate[bot] avatar tarao avatar tarao-scala-steward[bot] avatar windymelt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

record4s's Issues

Does not work in inline functions

It seems to me that record4s does not work correctly neither in inline nor transparent inline functions (the difference is important since they are inlined at different times iirc). Probably this is an issue with how the type checking of transparent functions works, so not strictly a bug in this library, but perhaps you can find a better workaround or escalate the issue to the Scala compiler. In any case, I hope that my report will at least serve as documentation for other users.

import com.github.tarao.record4s.%

%(name = "hello") + (bla = 123)

def foo() = {
  %(name = "hello") + (bla = 123)
}

inline def bar() = {
  %(name = "hello") + (bla = 123)
}

transparent inline def bob() = {
  %(name = "hello") + (bla = 123)
}

inline def foobar() = {
  (
    %(name = "hello").asInstanceOf[% { val name: String }]
      + (bla = 123)
  ).asInstanceOf[% { val name: String; val bla: Int }]
}

foobar()

Output:

val res0: com.github.tarao.record4s.%{val name: String; val bla: Int} = %(name = hello, bla = 123)

def foo(): com.github.tarao.record4s.%{val name: String; val bla: Int}

-- [E008] Not Found Error: -----------------------------------------------------
2 |  %(name = "hello") + (bla = 123)
  |  ^^^^^^^^^^^^^^^^^^^
  |value + is not a member of Any, but could be made available as an extension method.
  |
  |One of the following imports might make progress towards fixing the problem:
  |
  |  import math.Fractional.Implicits.infixFractionalOps
  |  import math.Integral.Implicits.infixIntegralOps
  |  import math.Numeric.Implicits.infixNumericOps
  |
1 error found

-- [E008] Not Found Error: -----------------------------------------------------
2 |  %(name = "hello") + (bla = 123)
  |  ^^^^^^^^^^^^^^^^^^^
  |value + is not a member of Any, but could be made available as an extension method.
  |
  |One of the following imports might make progress towards fixing the problem:
  |
  |  import math.Fractional.Implicits.infixFractionalOps
  |  import math.Integral.Implicits.infixIntegralOps
  |  import math.Numeric.Implicits.infixNumericOps
  |
1 error found

def foobar(): com.github.tarao.record4s.%{val name: String; val bla: Int}

val res1: com.github.tarao.record4s.%{val name: String; val bla: Int} = %(name = hello, bla = 123)

PS: Let me just say that this is an amazing library that doesn't have nearly as many stars as it deserves. It is not only surprisingly usable, but also serves as a good example of whitebox macro techniques ๐Ÿ‘

Integrate with jackson-databind

Integration with Jackson would be useful to be able to read and write YAML and TOML via jackson-dataformats-text.
I'll try myself if I have time, but I'm not familiar with any of Scala3/Record4s/Jackson, so I'd be happy to implement it for those who are familiar with them.
(After a little touch and go, it seemed difficult to avoid Type Erasure)

% and ArrayRecord violate "The general contract of hashCode".

record4s version

0.12.0

Reproduce code

import com.github.tarao.record4s.%
import com.github.tarao.record4s.ArrayRecord

% () == % ()
// res0: Boolean = true

% ().hashCode() == % ().hashCode()
// res1: Boolean = false


ArrayRecord() == ArrayRecord()
// res2: Boolean = true

ArrayRecord().hashCode() == ArrayRecord().hashCode()
// res3: Boolean = false

"The general contract of hashCode"

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Object (Java SE 21 & JDK 21)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.


Warning

Renovate failed to look up the following dependencies: Failed to look up github-releases package sbt/sbt.

Files affected: project/build.properties


Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

dockerfile
script/python/Dockerfile
github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-java v4
  • actions/setup-java v4
  • actions/setup-java v4
  • actions/upload-artifact v4
  • actions/checkout v4
  • actions/setup-java v4
  • actions/setup-java v4
  • actions/setup-java v4
  • actions/download-artifact v4
  • actions/download-artifact v4
  • actions/download-artifact v4
  • actions/checkout v4
  • actions/setup-java v4
  • actions/setup-java v4
  • actions/setup-java v4
  • scalacenter/sbt-dependency-submission v2
  • actions/checkout v4
  • actions/setup-java v4
  • coursier/setup-action v1
  • actions/checkout v4
  • actions/setup-java v4
  • actions/checkout v4
  • actions/setup-java v4
  • codecov/codecov-action v4
  • actions/checkout v4
  • actions/setup-java v4
  • actions/setup-java v4
  • actions/setup-java v4
  • peaceiris/actions-gh-pages v3.9.3
.github/workflows/clean.yml
.github/workflows/scala-steward.yml
  • scala-steward-org/scala-steward-action v2
  • ubuntu 22.04
.github/workflows/site.yml
  • actions/checkout v4
  • actions/setup-java v4
  • actions/setup-java v4
  • actions/setup-java v4
  • peaceiris/actions-gh-pages v3.9.3
sbt
build.sbt
  • scala 3.3.3
  • org.scalatest:scalatest 3.2.18
  • org.getshaka:native-converter 0.9.0
  • io.circe:circe-core 0.14.6
  • io.circe:circe-generic 0.14.6
  • io.circe:circe-parser 0.14.6
  • com.lihaoyi:upickle 3.2.0
  • scala 2.13.13
  • com.chuusai:shapeless 2.3.10
  • scala 2.11.12
  • ch.epfl.lamp:scala-records 0.4
  • io.circe:circe-core 0.14.6
  • io.circe:circe-generic 0.14.6
  • io.circe:circe-parser 0.14.6
project/build.properties
  • sbt/sbt 1.9.9
project/plugins.sbt
  • pl.project13.scala:sbt-jmh 0.4.7
  • org.typelevel:sbt-typelevel 0.6.7
  • org.typelevel:sbt-typelevel-scalafix 0.6.7
  • org.typelevel:sbt-typelevel-site 0.6.7
  • org.scala-js:sbt-scalajs 1.15.0
  • org.scala-native:sbt-scala-native 0.4.17
  • org.scoverage:sbt-scoverage 2.0.11

  • Check this box to trigger a request for Renovate to run again on this repository

Some questions

Hi,

I have a couple of questions - this would be a discussion rather than an issue... but didn't see discussions on the repo.

  1. Is there an obvious way to convert records into a scala JS, js.Object, js.Literal or similar? i.e. for interop with native JS libs?
  2. Would you consider merging an attempt at ujson integration?
  3. Would it be expected to play nicely with structural types?

This compiles, but throws at runtime.

import com.github.tarao.record4s.%
import reflect.Selectable.reflectiveSelectable

@main def playdoh =

  type BarPlottable = {
    val x: Int
    val y: Int
    val label: String
  }

  val t = %(x = 1, y = 2, label = "String")

  def typeTest(r: BarPlottable) =
    println(r.x)
    r.y
    r.label

  typeTest(t)

Compiles, but throws at runtime.

Full disclosure : My interest in your project, is that these sorts of records look dynamic enough to easily constructible from existing data, but static enough to provide the safety guarantees, that would helpful for my pet charting project.

Opaque type aliases are dealiased by Extensible.applyDynamicNamed

Minimized code

object A {
  opaque type Name = String
  object Name {
    def apply(name: String): Name = name
  }
}
import A.Name
val r1 = %(name = Name("tarao"), age = 3)
val r2 = r1 + (email = "[email protected]")
val name: Name = r2.name

Output

-- [E007] Type Mismatch Error: -------------------------------------------------
1 |val name: Name = r2.name
  |                 ^^^^^^^
  |                 Found:    String
  |                 Required: A.Name

Expectation

It should compile.

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.