Hi everybody and thanks in advance for your support
I'm making a university project in Lisp. The problem I have is that I don't know how to return the name of a function.
Let me explain
My project should be a Object Oriented Lisp, and I made everything. the only thing that doesn't work is method.
When I define a new class it has methods in it.
When I create a new instance of that class (for exapmple S1) I can make a new method in it which is a definition of a non existent method (for example a method named TALK). If I try to invoke that method with the name of that instance everything works (TALK S1 > "Hi, i'm S1"). If I create another instance (S2) with a method with the same name of TALK but with different implementation (for example TALK S2 > "My name is S2") it overwrites the other TALK, so if I try to invoke TALK S1 > "My name is S1".
In my project, everytime a function is defined it is not immediately defined with its true body, but with a standard body I defined. As soon as I invoke the function with the instance, the body should do spomething like this:
check if this method is defined in this instance or in his classes. if not then ERROR, else define that method with the body he finds in the instance and execute it; then redefine with the standard body. when I use this body I need the instance name (which is passed as soon as I invoke the method, ex TALK S1) but i also need the name of the function where the body is loaded, so i can check if it's defined in the instance or in his classes. But how can i do it with the need to write
talk 'talk s1 ?
I want to write
talk s1
and the program should do
>check function-name instance
where function-name is 'talk and instance is 's1
but it should be able to do the same if i write
>test-method q1
where here function-name is 'test-method and instance is 'q1
I hope i explained myself well, despite my bad english.
need help with Defun (not as simple as it sounds)
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: need help with Defun (not as simple as it sounds)
If I understood correctly, you created a Lisp dialect and you are now trying to define methods. It seems that you want to create methods on instances, not only on classes.
I believe there is more than one way to proceed. For instance, one possibility is to create an extra slot (field) in every class you create. This slot (which will be named, say, INSTANCE-METHODS) should contain an alist or hashtable with the methods defined for each instance. Then, for example, when you create the method TALK for S1, it would create the function
(or something like that) and put this function with the name of the method (in this case, TALK) into the slot INSTANCE-METHODS of S1, like this:
Then, a function named TALK would also be created (or superseded), it should look more or less like this:
This is obviously not clean or efficient, but it is a start. I hope my explanation is easy to understand, I'm not sure I can simplify it without knowing more details about your Object Oriented Lisp.
Good luck with your project. Not many people choose to create a language as a project
I believe there is more than one way to proceed. For instance, one possibility is to create an extra slot (field) in every class you create. This slot (which will be named, say, INSTANCE-METHODS) should contain an alist or hashtable with the methods defined for each instance. Then, for example, when you create the method TALK for S1, it would create the function
Code: Select all
(lambda (x)
"Hi, I'm S1")
Code: Select all
(push (cons 'talk
(lambda (x)
"Hi, I'm S1"))
(instance-methods s1))
Code: Select all
(defun talk (instance)
(let ((method (cdr (assoc 'talk (instance-methods instance)))))
(if method
(funcall method instance)
;; look for methods defined for the class of INSTANCE, otherwise signal an error
)))
Good luck with your project. Not many people choose to create a language as a project

Re: need help with Defun (not as simple as it sounds)
thanks for the reply, I'm going to check it as soon as possible and then I'll tell if I done well
Re: need help with Defun (not as simple as it sounds)
Thank you very much, you saved my life, it worked perfectly!
Re: need help with Defun (not as simple as it sounds)
Of course *that* works. I'll just have to update my tests!

MA



MA
Marco Antoniotti