Suppose I have two classes, each of them provides Builder API.

// This is in Ivy Script
import example.Person.Builder;

Builder errorPersonBuilder = new Person.Builder(); // this line generates a compilation error saying class Person.Builder not found.

Builder workingPersonBuilder = new Builder(); // this works.

It is still OK if there is only one Builder inside the context. As soon as there is another:

// This is in Ivy Script
import example.Person.Builder;
import example.Company.Builder; // compilation error of collision, there are two classes named Builder.


Builder workingPersonBuilder = new Builder(); // this works, see above.

Builder anotherBuilder = new Builder(); // which builder?

If Ivy Script could supports the same as normal Java, we could do:

import example.Person.Builder;
import example.Company.Builder;

Person.Builder personBuilder = new Person.Builder();
Company.Builder companyBuilder = new Company.Builder();

Should this be supported in Ivy Script?

asked 29.04.2016 at 07:13

Genzer%20Hawker's gravatar image

Genzer Hawker
(suspended)
accept rate: 66%


Hi

This code here:

import example.Person.Builder;
import example.Company.Builder;

Person.Builder personBuilder = new Person.Builder();
Company.Builder companyBuilder = new Company.Builder();

does not work in Java as well. The compiler says:

The import test.Person.Builder collides with another import statement

In java and IvyScript the following works:

import test.Company;
import test.Person;

Person.Builder personBuilder = new Person.Builder();
Company.Builder companyBuilder = new Company.Builder();

If you are working with builders, we usally provide a static method to start building. E.g.:

public class Person
{
  public static Builder build()
  {
    return new Builder();
  }

  public static class Builder
  {}
}

Then you do not have to import the Builder itself. Simple use:

Person person = Person.build().withName("Weiss").withGender(Gender.MALE).toPerson();
link

answered 29.04.2016 at 08:28

Reto%20Weiss's gravatar image

Reto Weiss ♦♦
4.9k202857
accept rate: 74%

Oh, i didn't pay attention to the import of the Java context. It's true. You are right! For the suggestion in your answer, yes, that's what I do to (combining builder & static factory method). However, some library and even JDK still just uses plain Builder. Thanks!

(29.04.2016 at 08:43) Genzer Hawker Genzer%20Hawker's gravatar image

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×33
×1

Asked: 29.04.2016 at 07:13

Seen: 1,920 times

Last updated: 29.04.2016 at 08:43