2

Consider this random object:

Object Test of Class TestClass:
  String Name;
  Integer Age;
  procedure setName(n);
  function getName(): String;

In Delphi if we want to work easily with many properties and methods of an object we can do this way:

Test.Name = 'EASI';
Test.Age = 34;
Test.setName('Eduardo Alcantara');
ShowMessage(Test.getName);

...or we can do it that way:

with Test do
begin
  Name = 'EASI';
  Age = 32;
  setName('Eduardo Alcântara');
  ShowMessage(getName);
end

Is there a similar structure in Java where we could shorten syntax like we can in Delphi?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
NaN
  • 8,596
  • 20
  • 79
  • 153
  • 2
    note that having a "with..do" construct is not a good thing in some peoples' eyes: http://stackoverflow.com/questions/514482/is-delphi-with-keyword-a-bad-practice – Argalatyr Jun 07 '14 at 01:26
  • In Delphi, people are still using **Delphi**? Not exactly a similar construct, but Java does have Bean shell - which allows you to use reflection and get some neat run-time type information. C# has the very similar "named parameters" but that's not really on-point to your question. – Elliott Frisch Jun 07 '14 at 01:29
  • 4
    @ElliottFrisch Yes, people are still using Delphi. It is still alive and breaking through the mobile world as well with the Firemonkey platform. – Jerry Dodge Jun 07 '14 at 01:38
  • @JerryDodge I have fond memories of Delphi. I just haven't seen it since the (late) 90s. – Elliott Frisch Jun 07 '14 at 01:51
  • 1
    @ElliottFrisch Delphi started at 90's end and nowadays it has more technology then C# if you do not know. It compiles to W32, W64, OSX, iOS and Android... – NaN Jun 07 '14 at 01:54
  • 1
    Fun fact: C# was created by the same guy who created Delphi, after he retired, Microsoft picked him up. – Jerry Dodge Jun 07 '14 at 02:10
  • @EASI C# covers all those platforms and more – David Heffernan Jun 07 '14 at 05:44
  • 2
    'with' is the Spawn of Satan. – Nick Hodges Jun 07 '14 at 19:47
  • not with `delphi` tag, where interested users may never find the way to that question if they do not know about vb.net language... – NaN Jun 08 '14 at 20:19
  • @EASI That's fine. Marking this question as a duplicate doesn't delete it. The point is that the answers are applicable. – Boann Jun 08 '14 at 21:55

1 Answers1

7

No, there is no similar construct in Java.


One Java-idiom is to use a chainable/builder-esque pattern:

Person person = new Person()
  .setName("Eduardo Alcântara")
  .setAge(32);
showMessage(person.getName());

However, this is not always applicable/appropriate and requires support by the type itself - mainly returning the receiver object (i.e. this) from the setter methods.

Note that the getName method is invoked against the original receiver/variable to avoid burying the side-effects in the showMessage call and that every Person method in the above example still has an explicit receiver (which is what the "do..with" construct can avoid).


Another approach that is sometimes used is Double Brace Initialization - this only works in context of a new statement and it creates a new anonymous inner class.

An advantage over the pattern above is that the setter methods need not be modified; an implicit receiver for the Person object only exists within the double braces, however.

Person person = new Person() {{
   setName("Eduardo Alcântara");
   setAge(32);
}};
showMessage(person.getName());

See also:

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    Nore that many Delphi users do not like the construct. In old Pascal, it was useful and could sometimes even save a few cycles when used on a simple record type. In current Delphi, where classes and records can have methods, and thus scoping is a little more complex, it is seen as a source of error by many, including me. – Rudy Velthuis Jun 07 '14 at 12:11
  • @RudyVelthuis I'm not such a fan of entirely unqualified member access as used by Delphi; *but* VB[.NET] has [WITH..END](http://msdn.microsoft.com/en-us/library/wc500chb.aspx) which I think is such a construct "done right" as it requires a leading `.` for access to the item members, such that there is no ambiguity. – user2864740 Jun 07 '14 at 19:51
  • I agree that anything that would distinguish the construct members from any other identifier in the scope could make "with" useful again. Note that Delphi also has with clauses with multiple (nested) subjects, which is, IMO, the most terrible construct in the language. – Rudy Velthuis Jun 08 '14 at 14:41