Results 1 to 10 of 10

Thread: Anonymous classes

  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,107
    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.

  7. #7
    Quote Originally Posted by SilverWarior View Post
    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.
    Well they don't demand you to use anonymous classes. You can always get by with making traditional classes. It is just a **** of a lot more convenient to use anonymous classes in some cases.

    I wasn't a big fan of this design approach when I first met it but I suppose I've gotten used to it by now. If I was designing an API for Pascal compilers I certainly wouldn't require the user to create a bunch of classes implementing interfaces with one or two methods in it. But for Java this has become a design approach used by many. I guess it has something to do with the mantra that everything must be a class and in stead of letting people extend large classes just to implement one method they let them implement a small interface which they can then pass as an argument.

    Anyway there are many advantages using the Java RT imo, especially when I can use Oxygene in stead of Java, so this curious design pattern isn't going to stop me from using Oxygene. Not even if I have to create a bunch of small classes with only one method in them
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  8. #8
    Based on your description this sounds as a way of how Java tries to implement generics.
    So maybe you should look in this way to find your answer faster.

  9. #9
    Quote Originally Posted by SilverWarior View Post
    Based on your description this sounds as a way of how Java tries to implement generics.
    So maybe you should look in this way to find your answer faster.
    Maybe my description sounds like I'm talking about generics but that is certainly not the issue. Java and Oxygene both support generics and they are used where appropiate.
    The use of anonymous classes I'm describing is more a method of simulating delegates.

    Anyway as I write earlier, it turns out Oxygene has partial support for this feature. I'll have to do with that.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  10. #10
    For whomever it may concern the latest update to Oxygene for Java now supports Inline interfaces inheriting from classes. Clearly I was not the only one desiring this feature.
    Now I have to seriously think about if I want to renew my subscription.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

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
  •