Go to Post everyone loves photoshops of Dave... - lukevanoort [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

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-02-2016, 12:51 AM
AMendenhall AMendenhall is offline
Registered User
FRC #3925
 
Join Date: Sep 2015
Location: Ventura
Posts: 24
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
  #2   Spotlight this post!  
Unread 07-02-2016, 05:26 PM
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 298
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: To extend Function or not to extend Function

Why do you need to restrict "imposter" classes? I think your second code block has the best solution.
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org
Reply With Quote
  #3   Spotlight this post!  
Unread 07-02-2016, 09:34 PM
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,536
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: To extend Function or not to extend Function

Quote:
Originally Posted by euhlmann View Post
Why do you need to restrict "imposter" classes? I think your second code block has the best solution.
Concur. The purpose of the interface is to allow a totally distinct implementation of the interface contract. While I agree that there is no clear way for an interface to enforce a contract, it is only trivially more difficult to "break the contract" for a class which extends a base class. (How many times have programmers overridden object.equals to mean something quite different than "is-another-handle-for-the-same-object"?)
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #4   Spotlight this post!  
Unread 07-02-2016, 11:05 PM
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 688
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: To extend Function or not to extend Function

+1 to what has already been said. It's up to each code implementor to ensure that the proper functionality is in place, and up to reach consumer of code that others have written to ensure that code does what they want.

To offer concrete proof that this isn't possible in Java:

Suppose there were a way to enforce what you're asking for
Code:
public interface A<T> /* magic that requires subclass of A to use itself as the type parameter */ {
public T interpolate(T a);
}
Since there's no way in Java to require a subclass to be final, nothing prevents a malicious user from doing the following
Code:
class B extends A<B> {
public B interpolate(B a){
// implementation
}
}

class C extends B {}
at this point, C is equivalent to
Code:
class C extends A<B>
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
Reply With Quote
  #5   Spotlight this post!  
Unread 07-03-2016, 08:00 PM
AMendenhall AMendenhall is offline
Registered User
FRC #3925
 
Join Date: Sep 2015
Location: Ventura
Posts: 24
AMendenhall is an unknown quantity at this point
Re: To extend Function or not to extend Function

Makes sense. Thanks for the reply
Reply With Quote
Reply


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:19 AM.

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