Results 1 to 10 of 10

Thread: Anonymous classes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Anonymous classes

    In Java it is not uncommon to run into anonymous classes looking something like this:

    Code:
    AnonymousClass aSpecificInstanceOfAnonymousClass = new AnonymousClass() { public void someMethod() { /* specific implementation */ }};
    Does anyone know if this is possible in Oxygene? I've tried copy some Java code looking like this through the Oxidizer and that didn't work so it may well not be possible. On the other hand the Oxidizer also fails to translate a line like this:
    Code:
    someObject.someFloat = 128f/255f;
    so maybe it is possible?
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  2. #2
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,108
    Blog Entries
    25
    Hmm interesting. I can look into it.

    What would you use it for specifically?

    Remember that Object Pascal is a very strongly typed language so this may contradict the way you are meant to code and may be a reason for it not being implemented. Yet RO does make a point of keeping their compilers 100% compatible with the platform so who knows.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  3. #3
    I've loooked around and Oxygene does support this. Partially. It's possible to implement an interface in an expression which will do in many cases:
    http://wiki.oxygenelanguage.com/en/Inline_Interfaces

    Oxygene does also have anonymous types, but they don't seem to be what I'm looking for:
    http://wiki.oxygenelanguage.com/en/Anonymous_Types

    To answer you question WILL, anonymous classes are useful when you need to implement an interface or extend a class but you will only need an instance of that class once. An example is the Comparator interface used for sorting. You often only need the comparator in one place and you only need to implement the method 'compare'. In these cases it can be nice to simply use an anonymous class written as an expression when you need it, in stead of going through the trouble of defining a new class and implement it. Anonymous classes also have the extra benifit that they have access to members in the scope they are defined in. Another example where anonymous classes come in handy is when you have to write Swing GUIs.

    Of course you shouldn't abuse anonymous classes. Your code can easily become unreadible and imposible to maintain if you use anonymous classes with 50+ lines of code all over the place. They should be used with care; but then they are great feature.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  4. #4
    Quote Originally Posted by pstudio View Post
    You often only need the comparator in one place and you only need to implement the method 'compare'. In these cases it can be nice to simply use an anonymous class written as an expression when you need it, in stead of going through the trouble of defining a new class and implement it. Anonymous classes also have the extra benifit that they have access to members in the scope they are defined in.
    Why would you create a new class which incorporates only one method and such method is only used on one place? Wouldn't it be easier to just write your method nested within some other method where it is needed?
    Based on my understanding the purpose of classes is the ability to join a group of methods which serves similar purpose into one object so that you can easily reuse this object on different places without the need to redeclare all of those methods.

  5. #5
    Quote Originally Posted by SilverWarior View Post
    Why would you create a new class which incorporates only one method and such method is only used on one place? Wouldn't it be easier to just write your method nested within some other method where it is needed?
    Based on my understanding the purpose of classes is the ability to join a group of methods which serves similar purpose into one object so that you can easily reuse this object on different places without the need to redeclare all of those methods.
    Java Collections and Arrays has built-in support for sorting. You can either implement the Comparable interface or provide an Comparator object if you need to sort custom objects based on more than one property. This is simply how the Java RT API is designed.

    Note also that you may wish to implement more than one method. For Swing GUIs you may e.g. create a bunch of classes implementing listener interfaces handling various input states.
    The nice thing with anonymous classes is that an empty method will be created for all those methods you don't implement yourself.

    The thing is, that this is common practise in Java, and some Java libraries basically expect you to use anonymous classes. If you don't use anonymous classes you are gonna end up with 100's of small classes (for a larger project) with one or two functions in it designed for one specific case.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  6. #6
    Quote Originally Posted by pstudio View Post
    The thing is, that this is common practise in Java, and some Java libraries basically expect you to use anonymous classes. If you don't use anonymous classes you are gonna end up with 100's of small classes (for a larger project) with one or two functions in it designed for one specific case.
    I didn't know that some libraries actually demmand annonymus classes in Java. But then again I do try to stay away from Java as I don't like its syntax. And now I also think I wouldn't like some approaches that are used in Java.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Comodo SSL