Code Monkey home page Code Monkey logo

Comments (9)

neilswingler avatar neilswingler commented on June 24, 2024 1

Thanks a lot. I tried it out already.

from freebuilder.

alicederyn avatar alicederyn commented on June 24, 2024

Is it necessary to not have the build method present? Otherwise you could just declare the legacy method in addition:

public Foo buildFoo() {
  return build();
}

from freebuilder.

neilswingler avatar neilswingler commented on June 24, 2024

In my use case I want to overload the actual build() method to return a different type so I require the generated builder to not declare a build() method at all.

I have a mutable data object with getters/setters and a hand coded builder that I hope will eventually be migrated to an immutable Freebuilder implementation but as a stepping stone I want to at least be able to use FreeBuilder for the builder part.

from freebuilder.

alicederyn avatar alicederyn commented on June 24, 2024

Can you post a simplified example of what that API would look like?

There are two methods on builders with the same signature (no parameters, returns a Foo), build and buildPartial, and while I can't think of any others users might want to add, it would be API-breaking to stop emitting build if one were added — but depending on the exact pattern you're making, it may still be possible to reliably detect it without breaking other use cases.

from freebuilder.

neilswingler avatar neilswingler commented on June 24, 2024

Could this work? It would not be a breaking change because currently, overloading the build() method return type leads to a compiler error.

@Freebuilder
interface Foo {
    class Builder extends Foo_Builder {
        public Bar build() { ... }  // incompatible return type => don't emit a build()
        public Foo buildFoo() { // compatible return type, emit buildFoo instead
            return super.buildFoo();
        |
    }
}

from freebuilder.

alicederyn avatar alicederyn commented on June 24, 2024

I would prefer to find a non-obscured method name matching a specific pattern for selecting the build method — perhaps this:

  1. If the build() method is protected or package-protected on the subclass, follow suit
  2. If the build() method is private or signature-incompatible, instead generate a package-protected _buildImpl() method for the subclass to call
  3. Optional for first implementation If that method is already present and incompatible, continue trying with _buildImpl2() etc. until a suitable candidate is found

This then cleanly extends to other methods in future, e.g. buildPartial() becomes _buildPartialImpl()

That would make your code look like:

@FreeBuilder
interface Foo {
    class Builder extends Foo_Builder {
        public Bar build() { ... }  // incompatible return type => FreeBuilder emits _buildImpl instead
        public Foo buildFoo() {
            return super._buildImpl();
        }
    }
}

I would be happy to accept a PR implementing this feature. If you are interested in doing this, let me know and I'll point you to the bits of the code you'd need to fit it into.

from freebuilder.

neilswingler avatar neilswingler commented on June 24, 2024

from freebuilder.

alicederyn avatar alicederyn commented on June 24, 2024

Gmail doesn't notify me of forum messages, so if you do pick it up, feel free reach out to me directly by email for a quicker response. Meanwhile, enjoy your holiday!

from freebuilder.

alicederyn avatar alicederyn commented on June 24, 2024

Hopefully FreeBuilder v2.7.0 will solve this issue for you. Please do raise another issue if it does not!

from freebuilder.

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.