Profile

Scrapbook photo 1
Scrapbook photo 2
Scrapbook photo 3
Scrapbook photo 4
Scrapbook photo 5
Pär Eklund
Works at Middleman Stockholm Consulting AB
Attended Uppsala University
Lived in Stockholm, Sweden
214 followers|261,023 views
AboutPostsPhotosYouTube

Stream

Pär Eklund

commented on a video on YouTube.
Shared publicly  - 
 
Beautiful song, terrible video (is it ironic / a joke?).
2
Add a comment...

Pär Eklund

Shared publicly  - 
1
Add a comment...

Pär Eklund

commented on a video on YouTube.
Shared publicly  - 
 
Until now I didn't realize her heart was as big as her talent (considering the audience and setting). Huge respect.
1
Add a comment...

Pär Eklund

Shared publicly  - 
1
Add a comment...

Pär Eklund

Shared publicly  - 
1
Add a comment...

Pär Eklund

Shared publicly  - 
1
Add a comment...
Have him in circles
214 people
Jenny Sundberg's profile photo
Lars Endre Morsund's profile photo
Carmen Mercer's profile photo
Karl Avedal's profile photo
Andy Heldenbrand's profile photo
Lisa Tucker's profile photo
Matt Hammond's profile photo
Klaus Krämer's profile photo
Radiy Efimov's profile photo

Pär Eklund

Shared publicly  - 
 
[Programming Question/Reply via email posted here for public access]

Generic class hierarchy and mutual recursive type bounds

Pär Eklund
Jan 31

to wadler 
Dear Mr. Wadler, ...

PURPOSE

I have a private project where I am implementing a class hierarchy in Java and want to use generics for optimal extensibility and type safety. 

THE NEED FOR MUTUAL RECURSIVE TYPE BOUNDS

In doing so, I quickly realised the need to parameterise each class with a type bound for each class known to said class, which directly leads to recursive type bounds such as:

public class Player
<
   L extends Level<L, P>, 
   P extends Player<L, P>, 
> { ...}

public class Level
<
   L extends Level<L, P>, 
   P extends Player<L, P>
> { ... }

The problem is that this quickly becomes unwieldly, since it requires modifying the header for every class in the hierarchy as soon as another class is added. Apart from the maintenance problem, comprehension is impeded which can be illustrated by the following example:

public interface Projectile
  <
    GC extends ActionGameComponent<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    S extends ActionGameComponentState,
    L extends ActionGameLevel<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    P extends ActionGamePlayer<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    EN extends Enemy<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    CO extends Collectable<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    A extends ActionGameActor<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    EX extends Exit<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    PR extends Projectile<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    CS extends Consumable<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    I extends Inventory<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    II extends InventoryItem<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    GU extends Gun<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    AM extends Ammo<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    VA extends Valuable<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    ENG extends GroundBasedEnemy<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    ENA extends AirBasedEnemy<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    WE extends Weapon<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>,
    IL extends InventoryListener<GC, S, L, P, EN, CO, A, EX, PR, CS, I, II, GU, AM, VA, ENG, ENA, WE, IL>
    >
  extends GameComponent<S, L> {
  A getOwner();
  int getDamage();
}

Seeing that only type bound A is actually used in the body, makes this feel even more "wrong" (even though I understand that the definition of the A type bound is recursively defined by the other type bounds).

Also, applying wildcards instead of type bounds to avoid the recursion will not work (to my experience and as I believe confirmed by your book Java Generics).

Thus my question: Would you know whether there is a better way to do this or if I have encountered a(n arguable) limitation in the Java Generics implementation?

Sincerely

/Par Eklund

Philip Wadler 
Jan 31

to Gilad, Maurice, me 
Thank you for your note. Yes, you have encountered a limitation in Java. One technique for dealing with these sorts of mutual bounds is to use type families, see this paper:

http://homepages.inf.ed.ac.uk/wadler/topics/gj.html
A statically safe alternative to virtual types

Kim B. Bruce, Martin Odersky, and Philip Wadler. European Conference on Object-Oriented Programming (ECOOP 98), Brussels, July 1998. Extended abstract in: Workshop on Foundations of Object-Oriented Languages (FOOL 5), San Diego, January 1998.
Parametric types and virtual types have recently been proposed as extensions to Java to support genericity. In this paper we examine both in order to investigate the strengths and weaknesses of each. We suggest a variant of virtual types which has similar expressiveness, but supports safe static type checking. This results in a language in which both parametric types and virtual types are well-integrated, and which is statically type-safe.

Available in: dvi, ps, dvi.gz, ps.gz, Springer web site.

You may also want to look at languages such as Beta and Newspeak, which make it easy to construct mutually dependent families of types. Yours, -- P

.   \ Philip Wadler, Professor of Theoretical Computer Science
.   /\ School of Informatics, University of Edinburgh
.  /  \ http://homepages.inf.ed.ac.uk/wadler/
1
Add a comment...

Pär Eklund

Shared publicly  - 
 
Very true
 
The real secrets behind Apple's success

Ever wondered why Apple is so popular? Hint: it's about a heck of a lot more than just good marketing -- though that certainly helps a lot. 
Ever wondered why Apple is so popular? Hint: it's about a heck of a lot more than just good marketing -- though that certainly helps a lot.
1
Add a comment...

Pär Eklund

commented on a video on YouTube.
Shared publicly  - 
 
To a parent, perhaps the most powerful and sad song ever written. 
6
Add a comment...

Pär Eklund

Shared publicly  - 
1
Add a comment...

Pär Eklund

Shared publicly  - 
1
Add a comment...

Pär Eklund

Shared publicly  - 
1
Add a comment...
People
Have him in circles
214 people
Jenny Sundberg's profile photo
Lars Endre Morsund's profile photo
Carmen Mercer's profile photo
Karl Avedal's profile photo
Andy Heldenbrand's profile photo
Lisa Tucker's profile photo
Matt Hammond's profile photo
Klaus Krämer's profile photo
Radiy Efimov's profile photo
Work
Occupation
Self-employed systems architect with 15 years experience. Focus on server-based and distributed systems, mainly on the Java platform and other open technologies
Employment
  • Middleman Stockholm Consulting AB
    Founder/CEO/Systems Architect, 2001 - present
Places
Map of the places this user has livedMap of the places this user has livedMap of the places this user has lived
Previously
Stockholm, Sweden
Links
Contributor to
Story
Introduction
I live in Sweden with my wife Jessica and new-born son Gustav in a house close to the capital Stockholm. 

My main interests and hobbies are computer science (mostly systems architecture and development), weightlifting, horseback riding and skiing. 

Lately however, most of my time is devoted to home repair, gardening, taking care of my new-born son and other things to keep the family happy. :) 

Other than that, I love animals and intend to make them a larger part of my life, more concretely getting a cat, a dog (a rottweiler when my son gets a little older) and to go horseback riding more regularly. My non-domestic favorites will have to stay in the wild. :)
Bragging rights
Spent a night in a military locker. Hugged a lion. Mingled with a pack of wolves at 5 different occasions.
Education
  • Uppsala University
    Computer Science, 1992 - 1996
  • University of Texas at Austin
    Computer Science, 1995 - 1996
Basic Information
Gender
Male
Apps with Google+ Sign-in