Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Call custom methods on method parameters of type Object
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default Call custom methods on method parameters of type Object - 03-31-2007, 05:53 PM

Hopefully, someone will tell me that the following interpretation is incorrect, and show me how to do what I want to do.

Assume that I define a new world-level method with one parameter of type Object.

Assume that I call that method from another method, passing a penguin object as a parameter.

Gettin back to the code in the new method, to call methods on the incoming parameter, I need to drag it down and drop it into an appropriate location in the edit pane. Is this the correct procedure, or is there another way to call methods on the incoming parameter?

If I drag the parameter and drop it into my edit pane, a popup menu appears giving me access to about 16 of the 20 primitive methods that apply to all objects, plus a general set value method and five set methods for setting standard property values (color, opacity, etc.) on the incoming object. The remaining four primitive methods are essentially redundant, so I don't mind losing access to them.

However, a penguin object also contains about eight custom methods such as glide, jump, and walk. I don't seem to have access to those methods. This does bother me because I would like to use those methods in the new method that I have written.

As an OOP programmer, I understand exactly how this can happen, since the incoming object is being treated as the generic type Object instead of the more specialized type Penguin. Unfortunately, the dialog for creating a new method doesn't allow me to specify the incoming parameter type as the class Penguin.

In a more conventional OOP language, if I brought the object in as a generic (superclass) type such as Object, I would need to cast the object from type Object to type Penguin in order to get access to the specialized methods that are defined in the Penguin class. However, I can't figure out how to perform a cast in Alice, and I don't know if it is even possible.

I can call the general set value method on the incoming parameter to set its value to the actual penguin object that was passed as a parameter (or any other existing object for that matter), but I can't figure out how to take advantage of that procedure to call the custom methods that belong to the penguin object. In any event, that doesn't seem like the right thing to do because the code in the new method shouldn't have any idea which specific object was actually passed as a parameter in the method call. It might not even be a penguin. It might be a cow or any other object instantiated from a class in the gallery.

Any help toward a solution to this problem will be appreciated.

If my gloomy interpretation is correct, I will appreciate a confirmation of that fact so that I can quit looking for a solution.

Thank you.
Dick Baldwin
   
Reply With Quote
Methods with parameters of type object
Old
DrJim
Guest
 
Status:
Posts: n/a
Default Methods with parameters of type object - 03-31-2007, 08:16 PM

Not sure this answers any or all of your question but maybe it’s enough for a start – if I totally missed the point, let me know. Just because the option doesn’t appear on the pulldown menu doesn’t always mean it doesn’t exist or that you can’t use it.

In the specific case of methods, I think you can always use a method with any object, regardless of the object it was initially associated with – assuming that all of the subobjects called out in the method exist for that object and have exactly the same full name (i.e., all names down the hierarchy, except the top, must be identical).

The attached world shows an example. The method is essentially a copy of the penguin’s turn_head method. To use it with Bob, I had to rename his “upper body” to head, which doesn’t give the ideal result as far as the character is concerned but about all I think you can get without redoing the model.

The real problem with trying to use methods like these with multiple objects is that very few of the Alice objects were developed with a common hierarchy (the fairies and some of the students are exceptions). Since there is no (easy) way to edit the hierarchy of any of the objects, you usually run into even worse problems than I had in this case.
Attached Files
File Type: a2w CustomMethodTest.a2w (1.65 MB, 401 views)
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 03-31-2007, 08:48 PM

I attempted to attach an HTML file containing the source code for your world to learn how to do that sort of thing with this forum software.

How did you construct the statement that begins with the following:

Dancer .partNamed( head )

I haven't found anything having to do with a part of the incoming object.

Thanks,
Dick Baldwin
   
Reply With Quote
I found the partNamed function
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default I found the partNamed function - 03-31-2007, 09:22 PM

DrJim

I found the partNamed function following my previous posting.

I will have to think about that. At first blush, it appears that the author of the new method would have to know in advance the types of objects as well as the names of their parts that will be passed to the new method in all cases. This seems to violate the OO concept that I am working toward but it may be a way to get the job done. I understand that Alice lacks lots of OO features, but it is still a great teaching tool.

Thanks for your response.

By the way, I failed to successfully attach the HTML file. Can you list the steps required to successfully attach a file?

Thanks,
Dick Baldwin
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 03-31-2007, 09:35 PM

Well, DrJim, even after finding the partNamed function, I still can't figure out how you constructed the following expression:

Dancer .partNamed( head )

What is the drag-and-drop procedure for calling the partNamed function on Dancer, which is the local name of your incoming parameter of type Object? How do you get access to a list of functions that belong to the generic type Object?

Thanks,
Dick Baldwin
   
Reply With Quote
Restating the question
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default Restating the question - 03-31-2007, 10:18 PM

I'm going to start over and restate my question. I have attempted to attach a very simple world that illustrates the situation. In case that fails, here is a copy of a listing of the source code. This listing was produced using the Java-like output option:

world
Methods
public void main ( ) {

wait( 1 second );
world.closeOrderDrill ( theObject = penguin1 );
}


public void closeOrderDrill ( Object theObject) {

theObject .move( FORWARD , 1 meter );
// Would like to replace above statement with:
// theObject.walk(move_time = 3);
// or something equivalent.
}

I am passing a penguin object to the method named closeOrderDrill.

The penguin has a custom method named walk.

The penguin is received by the closeOrderDrill method as type Object.

Knowing that the incoming object is really type Penguin, I would like to call the walk method on the incoming object. Is that possible, and if so, how do I do it?

=====A related question for DrJim========

Although I don't believe necessarily that it is the solution to my problem, I would also like to know how you constructed the following expression in your sample world about the two dancers.

Dancer .partNamed( head )

What is the drag-and-drop procedure for calling the partNamed function on Dancer, which is the local name of your incoming parameter of type Object? How do you get access to a list of functions that belong to the generic type Object?

Thanks,
Dick Baldwin
Attached Files
File Type: a2w Alice0145a1.a2w (529.7 KB, 373 views)
   
Reply With Quote
Partial Answer
Old
DrJim
Guest
 
Status:
Posts: n/a
Default Partial Answer - 03-31-2007, 11:28 PM

Will just hit the “easy” questions tonight – it’s too late to deal with anything that has parenthesis in it so will look at that question tomorrow. (Later Note: April Fool's Day additonal comments in the following positing.)

1. When I’m having trouble with uploads, I generally just zip the file rather than trying to figure out the problem – the size limits on zip files are adequately large and there don’t seem to be any other limits. For some reason, .html files are not a supported upload type - probably because you really do need to upload a directory.

2. I just entered the “head” by selecting “other” and typing the part name in as a string. Note you have to do the full part name, which can get long, i.e., “leftarm.leftforearm.lefthand.leftthumb” etc. Don’t know an easier way – never found a drag and drop approach that worked but there may be one. There is an example of this approach in one of the texts but I couldn’t find it with a quick glance.

3. As far as what methods and functions belong to the type “object” – I’ll leave that to someone else. (It would be nice to have a reference specification that covers that type of stuff.) I just drag in something from an existing object and see if I get an error message – nearly all the time I don’t.


Quote:
Originally Posted by DickBaldwin View Post
At first blush, it appears that the author of the new method would have to know in advance the types of objects as well as the names of their parts that will be passed to the new method in all cases. This seems to violate the OO concept that I am working toward but it may be a way to get the job done. I understand that Alice lacks lots of OO features, but it is still a great teaching tool.
4. Think all you have to know is that the objects you are going to be working with have the parts (and know the name for the part) the method is trying to operate on. From a practical standpoint, I don’t see how to get around this – if I did I’d flap my wings with joy. I’m the wrong person to comment on OO issues but I suspect this is all tied up in the question of support for polymorphism – which is an admitted problem with the current version of Alice.

Jim

Last edited by DrJim; 04-01-2007 at 02:56 PM. Reason: Added reference to next posting.
   
Reply With Quote
Old
DrJim
Guest
 
Status:
Posts: n/a
Default 04-01-2007, 02:53 PM

I may still be missing something, but I think the attached world at least is a start towards what you were trying to do (or at least about as close as you can get in Alice).

The attached picture is a joke - but it does really illustrate the basic idea. The only way I know to create a new "class" in Alice is to place a new object into the world, modify it as desired and then either save it or duplicate it for other objects of the same class.

The world in the picture won't work for multiple objects because I didn't modify the walk method to work with a parameter instead of on a specific object. That's a rather straightforward change (which I made in a couple of places in the attached world), if time consuming (why I didn't complete all of the changes).

Note that I "cloned" the son before I made the changes in the method so he didn't inherit them - bad idea because now if you delete him it screws up a lot of code.

I think from a practical standpoint, this does what you want. From a purist standpoint, it's very difficult to say what objects belong in the new "class" - lots of things will work with some of the "class methods" but only objects with exactly the same sub-object hierarchy (including names) will always work. Seems like a nice way to get into deep trouble - would like to hear if you come up with a better approach.

Jim
Attached Images
File Type: jpg Penguin.jpg (48.6 KB, 312 views)
Attached Files
File Type: a2w DrJimsBLOs.a2w (481.4 KB, 374 views)
   
Reply With Quote
Old
DickBaldwin
Guest
 
Status:
Posts: n/a
Default 04-01-2007, 04:49 PM

Hello DrJim,

While the world that you attached to the previous posting contains an interesting approach, I believe that it defeats the fundamental concept of encapsulation.

Also, although I haven't confirmed it yet, I believe that this issue applies not only to world-level methods, but to class-level methods as well.

Consider this example. You have been tasked to write the code for a new class to be placed in the gallery to be used by many students for many years to come. One of the methods in the class that you will write must receive an incoming object as a parameter of type Object and call one of the methods belonging to that object.

Assuming that your system doesn't support runtime polymorphism, all you should need to know about the incoming object is its true type (Penguin for example) and the signature of the method that you will need to call, such as a method named walk with a single parameter of type Number. (You shouldn't even need to know or care what will happen when you call that method and pass a parameter of the correct type.)

Under no circumstances should you need to know the name that will be given to the actual object that will be passed to your method by some student three years hence in order to write the method in the new class now.

The following statement in your method requires you as the author of the new class to know the name that will be given to the object in the calling method:

DrJim'sBLO.walk ( move_time = 3 , WakingObject = theObject );

Further, by including the student's name for the object in your method, you are requiring all students who use your class and call your method in the future to use that name for the object that they will pass to your method. If a student needs to pass the same object to two different methods in two different classes and is required by each class to give the object a different name, that student will have a real problem.

On the flip side, it was necessary for you to modify the method belonging to the object to make it work even knowing the name given to the object by the student. It should never be necessary to modify a method belonging to an object in order to call that method, provided you have appropriate access to the object to which the method belongs, you know the true type of the object, and you know the signature for the method. (With runtime polymorphism, you wouldn't even need to know the true type.)

In a more conventional OO language, without making use of polymorphism, all that would be necessary to invoke any method belonging to the incoming object would be to use a cast operator to temporarily change the type from type Object to the actual type of the object (Penguin for example). This assumes that you know the true type of the object as well as the signature for the method that is to be called.

Please don't think that I'm being critical of your work. I'm not. Under the circumstances, you came up with a unique solution. You must truly be one of world's experts on Alice programming.

However, given your extensive knowledge of Alice, I'm going to take this as a confirmation that there is no simple and straightforward solution in Alice to what is almost trivial in more conventional OO languages.

Despite all that, I still consider Alice to be an excellent teaching tool that can be used to teach students about encapsulation, and a little about inheritance (in a strange sort of way). This will surely get them started down the road to OOP and let them have some fun in the process.

I'm currently leading an effort at the college where I teach to change from C++ to Alice for our Programming Fundamentals course. Part of what I am doing is trying to identify potential pitfalls before someone who disagrees with the change uses a potential pitfall in an attempt to defeat the change. That is why I pursued this problem in such great detail.

By the way, although I haven't researched it yet, from what I have been reading, this same problem applies to objects that are stored in Lists in Alice, so this isn't simply a method issue. Apparently it is a data structures issue as well.

Thank you very much for your excellent feedback.
Dick Baldwin
   
Reply With Quote
Old
DrJim
Guest
 
Status:
Posts: n/a
Default 04-01-2007, 09:36 PM

Quote:
Originally Posted by DickBaldwin View Post
Hello DrJim,
.... You must truly be one of world's experts on Alice programming.
That's flattering - but far from true (at least I hope so). For what you are interested in, I suggest you look at Joel Adam's new book (see LanceA's comments at http://www.alice.org/community/showt...=2153#post2153 ).

I'm making this recommendation without actually having seen the book itself - it's quite pricey and frankly I'm discouraged enough with the current version of Alice that I didn't feel like investing in "yet another textbook" on version 2.0 when this is just a hobby interest for me. I do feel the CMU folks are aware enough of the problems that the next version of Alice will be better (taking a page from Microsoft and others - "Much will be fixed in the next version.")

Additionally don't assume that because I couldn't come up with a better solution, one doesn't exist. Maybe Chuck Herbert or someone else will post a better approach.

Your comments on my approach are both accuate and well stated. In case I wasn't clear on the point, I didn't/don't consider it a good solution in any sense other than the most basic criteria - it does work.

I don't teach programming and don't plan to - but if I did, a big concern with the present version of Alice would be that it almost forces one to accept this minimal definition of "good programming" - and makes even that level of success almost impossible to obtain with such basic tasks as creating Java applets. That has to be discouraging to a student - especially since Alice appears to be very effective in generating a high level of initial interest in a general student population. Even worse, it might discourage them from actually continuing and learning the skills needed to develop truly quality programs.

Jim
   
Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2020, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.