Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Anonymous functions (http://www.chiefdelphi.com/forums/showthread.php?t=115291)

Arhowk 22-03-2013 21:51

Anonymous functions
 
I want to do something like this

Code:

private ExtendedBoolean b = new ExtendedBoolean(){return m_pressed};
private void run()
    if(b){
        motor.set(1);
    }
}

but the only way i can do it now is

Code:

private ExtendedBoolean b = new ExtendedBoolean(){private boolean get() return m_pressed;};
private void run()
    if(b.get()){
        motor.set(1);
    }
}

which just looks ugly in my opinion. Is there a good way to do it?

armgeek 31-03-2013 20:24

Re: Anonymous functions
 
This is a "good" way to do it.

However, you could make it look a lot "neater" if you changed the "get" method's name to "pressed". That way the if statement would read more like English would.

Ginto8 31-03-2013 21:06

Re: Anonymous functions
 
I'm generally of the opinion that little bits of ugliness like that are necessary evils in programming. While I'd love it if everything could be simply and elegantly expressed, reality tends to disregard my desires. However, the example you gave was such that the only non-"ugly" solution was what you suggested, which isn't possible in Java. Depending on what you're trying to do (as opposed to how you're trying to make it look), there might actually be an elegant solution.


All times are GMT -5. The time now is 11:35.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi