Go to Post Before joining FIRST, I sure wanted to be SUPERMAN or SPIDER MAN!....but after joining FIRST, I realized that there are other ways I can help people and the world. - RoboMadi [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 02-07-2016, 00:51
AMendenhall AMendenhall is offline
Registered User
FRC #3925
 
Join Date: Sep 2015
Location: Ventura
Posts: 28
AMendenhall is an unknown quantity at this point
To extend Function or not to extend Function

Consider the following case:
You want to make an interface which forces the functionality of being interpolable, that is, being able to be interpolated. You'd probably start off with the following code
Code:
public interface Interpolable {
    /**
     * Interpolates this value and @param a
     */
    public Interpolable interpolate(Interpolable a);
}
Herein lies the 1st problem:
If the interpolate method accepts a, of type Interpolable, and returns some Interpolable, then any implementing class could accept and return any other implementing class. Desired functionality would limit both of these to only the type of the class which is doing the implementing.
My solution:
Code:
public interface Interpolable<T extends Interpolable<T>> {
    /**
     * Interpolates this value and @param a
     */
    public T interpolate(T a);
}
This way, any class which implements interpolable must specify that the generic type T is itself, that is, the method interpolate accepts and returns only itself. Problem is, an imposter class could lie and say that it accepts and returns a different interpolable type. Is there a more elegant way to do this?

Say you also want your Interpolable interface extend UnaryOperator, because it takes in an Object and returns an Object, both of which are of type T. However, the method required by UnaryOperator is T apply(T a). A way to get around that would be to make Interpolable an abstract class and make the method interpolate call apply. Are there more elegant ways to do this?
Code:
public abstract class Interpolable<T extends Interpolable<T>> implements UnaryOperator<T> {
    /**
     * Interpolates this value and @param a
     */
    public T interpolate(T a) {
        return apply(a);
    }
}
I know this isn't super FRC oreinted, but I'm still using it for some cool robotics stuff that I think I'll release sometime this summer. Thanks for any replies.
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 08:29.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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