Skip to main content

[Language enhancement] Class scope

2 replies [Last post]
eliott
Offline
Joined: 2009-06-03

Hi,

I'd just like to suggest a language enhancement I'd like to see in Java; I don't know if this has been suggested before, so sorry if I'm repeating something. Or maybe it hasn't been mentioned as there already exists a way to achieve it that I don't know about, again sorry.

One thing I believe is missing from any OO language I've ever seen.. I fail words to come up with a name for such a thing ("Class scope" I said in the subject is a bad name), so I'll just describe it..

If I have a class 'Dog', it describes Dogness in every detail. This may include actions such as scratchBehindEars(), playFetch() and untwistStomach() (a Simpons reference; I'm not suggesting it's typical to untwist a dog's stomach). Anyway, these are what I'm saying can be done to any dog. The suggestion I'd like to make is that not all people view dogs like this: a dog owner is only interested in the actions scratchBehindEars() and playFetch(), where a veterinary surgeon will be interested in, well scratchBehindEars() why not, and untwistStomach(). So, to me, it seems like a fundamental thing that's missing from OO: that the owner and surgeon always see the dog the same way. How the dog is viewed, of course, has no effect on the dog, it's still a dog (so this is nothing to do with type-checking, just scoping)

Here's a little code example---haven't really thought the syntax through, whether it could be parsed (and after reading it's probably a bit off), but the general idea is..

////// Dog.java //////
public interface Dog
{
public (Owner, Surgeon) void scratchBehindEars();
public (Owner) void playFetch();
public (Surgeon) void untwistStomach();
}

////// Greyhound.java //////
public class Greyhound implements Dog { ... }

////// Main.java //////
public class Main
{
public static void main(String[] args)
{
Dog(Owner) santasLittleHelper = new Greyhound(); // scope is only described in the receiving type; cannot create an object with a particular scope
santasLittleHelper.scratchBehindEars();
santasLittleHelper.playFetch();
santasLittleHelper.untwistStomach(); // Error: symbol out of scope (or whatever)
giveToVet(santasLittleHelper);
}
static void giveToVet(Dog(Surgeon) dog)
{
dog.scratchBehindEars();
dog.playFetch(); // Error: symbol out of scope
dog.untwistStomach();

// And there's nothing to stop the surgeon suddenly stealing the dog and becoming the owner
// That is, this is not like dynamic casting (a dog is always just a dog) it's just changing the scope (maybe a different syntax is needed)
((Dog(Owner))dog).playFetch();

// Maybe a different syntax like..
dog(Owner).playFetch();

}
}

Well, that's basically it, and I only see it being needed for public access.

Let me know what's thinking,
Thank you,
Kind regards,
Eliott

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
peter__lawrey
Offline
Joined: 2005-11-01

You can do this with more than one interface

public interface OwnerDog {
public void scratchBehindEars();
public void playFetch();
}

public interface SurgeonDog {
public void scratchBehindEars();
public void untwistStomach();
}

public interface Dog extends OwnerDog, SurgeonDog {
}

The owner can get the OwnerDog and the surgeon can get the SurgeonDog

eliott
Offline
Joined: 2009-06-03

Hi,

Thank you for the response.

An interesting hack, though I still feel an OO language needs a real construct for this. Philosophically, the context in which an object is used is just as important as the object's actions, queries, etc. (currently reflected in OO) that make the object. And in practise, large complex objects exposing everything they offer all the time is confusing and bug prone.

Your solution doesn't allow the OwnerDog to be given to the surgeon (as an OwnerDog is not a SurgeonDog).

Still, interesting and thank you again,
Kind regards,
Eliott