GithubHelp home page GithubHelp logo

API change suggestions about elastic4s HOT 7 CLOSED

philippus avatar philippus commented on June 1, 2024
API change suggestions

from elastic4s.

Comments (7)

guersam avatar guersam commented on June 1, 2024

If you agree I'll open a draft PR.

from elastic4s.

sksamuel avatar sksamuel commented on June 1, 2024

Shall we do these in separate PRs ?

What's the advantage of bringing the inner classes to top level?

from elastic4s.

guersam avatar guersam commented on June 1, 2024
  1. At first, I'd like to point out inner classes become different concrete ones according to the class hierarchy, for example:

    trait A { case class Inner(v: Int) }
    object B extends A
    object C extends A
    
    B.Inner(1) == B.Inner(1) // true
    B.Inner(1) == C.Inner(1) // false

    It's because B.A and C.A have different FQCNs. Single FQCN has following benefits other than equality checking:

  2. Implicit resolution without import tax

    class RichInt(a: Int) {
      def plus(b: Int) = a + b
    }
    
    object RichInt {
      implicit def apply(a: Int): RichInt = new RichInt(a)
    }
    
    def plus1(a: RichInt) = a.plus(b)
    
    plus1(23) == 24 // true 

    We still need import RichInt._ to directly use 1.plus(3), so we need to exposure some implicit conversions for the entry points at least. But after that we can provide well-organized implicit conversions as well as type safety.

    I used this technique for Redisreact to provide typesafe, out-of-box serialization not relying on (_: Any).toString. I'd like to bring it to Elasitc4s as well in near future.

  3. Avoid boxing/unboxing overhead using Value classes

    By extending AnyVal we can avoid runtime overhead caused by creating tons of intermediate wrapper instances. However, it's not supported for inner classes:

    scala> class A
    // defined class A
    
    scala> class B(val a: A) extends AnyVal
    // defined class B
    
    scala> class Outer { class C(val a: A) extends AnyVal }
    //<console>:8: error: value class may not be a member of another class
    //       class Outer { class C(val a: A) extends AnyVal }
    //                           ^

from elastic4s.

guersam avatar guersam commented on June 1, 2024

Actually it's not necessary to make it top-level, the point is to give it single FQCN. So it's ok in the previous example:

object Outer { class C(val a: A) extends AnyVal }
// it works because there's only one FQCN for `Outer.C`

from elastic4s.

sksamuel avatar sksamuel commented on June 1, 2024

Ok I see. What classes are you wanting to be top level? Happy for you to do a PR.

from elastic4s.

guersam avatar guersam commented on June 1, 2024

Couldn't check all of them yet, but basically want to avoid any unnecessary path-dependent class.

I'll do a draft in one or a few weeks, so let's discuss more on that PR.

from elastic4s.

sksamuel avatar sksamuel commented on June 1, 2024

Ok lets do that. 0.90.4 is out this week or next so will be doing the next release for that.

from elastic4s.

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.