From: ··········@gmail.com
Subject: few confusions regarding CLOS
Date: 
Message-ID: <1176145029.922745.228910@q75g2000hsh.googlegroups.com>
Hello everybody,

I am new to the lisp and clos. while reading clos, i have found myself
in deep confusion. i came from c++ background of oop, so i am looking
clos from that perspective.

I have following doubts, please give me some insight:-

1. are generic functions lisp counter part of function overloading?
2. what is exactly multiple dispatch(i tried to search a gud tutorial
for this, but,....)
3. Can I have member functions for any lisp class?
4. how can i achieve dynamic polymorphism using CLOS?

thanks in advance.
-Tushar

From: jayessay
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <m36485gugd.fsf@sirius.goldenthreadtech.com>
···········@gmail.com" <··········@gmail.com> writes:

> Hello everybody,
> 
> I am new to the lisp and clos. while reading clos, i have found myself
> in deep confusion. i came from c++ background of oop, so i am looking
> clos from that perspective.

You are going to have a hard time if you keep this mindset.  Really,
forget about C++ and just focus on the machinery of CLOS and what it
does and you will do much better.  Suggestion: pick up a copy of
Keene's _Object Oriented Programming In Common Lisp_.


> I have following doubts, please give me some insight:-
> 
> 1. are generic functions lisp counter part of function overloading?

No, function overloading (aka static "dispatch"/polymorphism) doesn't
exist in Lisp (well you can hack it up, but I would guess no one ever
does this).

Generic functions are the mechanism providing dynamic
dispatch/polymorphism.  They define a protocol for a family of
methods.  An individual method will belong to such a family (it
"belongs" to the generic function) But don't this as virtual functions
or you will end up totally confused.


> 2. what is exactly multiple dispatch(i tried to search a gud tutorial

Just what it says.  First, methods do not have a distinguished
parameter on which they are dispatched.  Generic functions dispatch
their methods based on resolving any number of parameters (multiple)
to the specific signatures of their methods.  If the resolution across
the multiple parameters succeeds, the method identified as satisfying
it is dispatched.  But just because you can have multiple parameter
resolution (and dispatch) doesn't mean you have to - one can be used
just fine 


> 3. Can I have member functions for any lisp class?

You cannot have member functions for _any_ Lisp class.  Why?  Because
member functions do not exist (i.e., classes do not contain functions
of any kind).  Further, as noted above, methods "belong to" generic
functions, not classes.

Name space control is done with packages.  Please note this also (as
it's sure to confuse you as well): Packages control the visibility of
_names_ (symbols) not the things they name.


> 4. how can i achieve dynamic polymorphism using CLOS?

Generic functions - see above.


/Jon

-- 
'j' - a n t h o n y at romeo/charley/november com
From: Luís Oliveira
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <m1ps6dkym4.fsf@deadspam.com>
jayessay <······@foo.com> writes:
> Suggestion: pick up a copy of Keene's _Object Oriented Programming In
> Common Lisp_.

I haven't read that book so I can't recommend it, but I enjoyed learning
CLOS from "Practical Common Lisp", chapters 16 and 17:

  <http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html>
  <http://www.gigamonkeys.com/book/object-reorientation-classes.html>

-- 
Luís Oliveira
http://student.dei.uc.pt/~lmoliv/
From: Dan Bensen
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <eveb9b$5u6$1@wildfire.prairienet.org>
··········@gmail.com wrote:
> I am new to the lisp and clos. while reading clos, i have found myself
> in deep confusion.

You're not alone :)

There are two recent threads on this topic here on c.l.l:
* "Porting Ruby snippet to Lisp" started 3/17
* "objects & namespaces"         started 2/20

 > i came from c++ background of oop, so i am looking
> clos from that perspective.

Beware:  CLOS is very different from C++.  A CLOS object is sort of
like a simple struct with inheritance.  There are no member functions.

> 1. are generic functions lisp counter part of function overloading?

Effectively, yes.  Dispatching is sort of like runtime overloading.
What a generic or overloaded function does depends on the types of
its arguments.

As Rainer explained, though, there's a big difference.  A generic
function is a real function that selects the appropriate method
at runtime rather than compile time.

> 2. what is exactly multiple dispatch(i tried to search a gud tutorial
> for this, but,....)

Dispatch means the generic function decides which method to "dispatch"
on the arguments depending on the argument types.  Multiple dispatch
means the generic function can dispatch different methods depending
on the types of all its arguments, not just the first one.

In C++, you think of a member function as being contained inside the
class it's defined in.  But in reality, that's just syntactic sugar
for calling different functions based on the type of the object that
the function is a "member" of.  Underneath the hood, the object is
really just the first argument of the function.

So you can think of
   "MyObject.MyFunction(args)"
as
   "MyFunction(MyObject,args)",

which is a function that's overloaded based on its first argument.
This is called "single dispatch" because it only dispatches on
the first argument, ie. the object whose class the function is
defined in.  CLOS generic functions dispatch on all their arguments,
not just one, so they're an example of multiple dispatch.

> 3. Can I have member functions for any lisp class?

As you might have guessed by now, the answer is NO.  All
class-specific operations on CLOS objects are performed by
external methods, which are called by generic functions.

> 4. how can i achieve dynamic polymorphism using CLOS?

Dynamic polymorphism is automatic in dynamically typed languages.
Just define a function without specifying its argument types, and
it will be polymorphic.

-- 
Dan
www.prairienet.org/~dsb/
From: Rainer Joswig
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <joswig-73178B.23542809042007@news-europe.giganews.com>
In article <············@wildfire.prairienet.org>,
 Dan Bensen <··········@cyberspace.net> wrote:

> ··········@gmail.com wrote:
> > I am new to the lisp and clos. while reading clos, i have found myself
> > in deep confusion.
> 
> You're not alone :)
> 
> There are two recent threads on this topic here on c.l.l:
> * "Porting Ruby snippet to Lisp" started 3/17
> * "objects & namespaces"         started 2/20
> 
>  > i came from c++ background of oop, so i am looking
> > clos from that perspective.
> 
> Beware:  CLOS is very different from C++.  A CLOS object is sort of
> like a simple struct with inheritance.  There are no member functions.

Plus objects have an identify, one can change the slots at
runtime and one can change the class of the object at runtime.

This is part of what Common Lisp makes a 'object-oriented dynamic'
programming language. The 'dynamic' has little to do with
dynamic typing, it means that the object system
has features for dynamic change at runtime.

> 
> > 1. are generic functions lisp counter part of function overloading?
> 
> Effectively, yes.  Dispatching is sort of like runtime overloading.
> What a generic or overloaded function does depends on the types of
> its arguments.
> 
> As Rainer explained, though, there's a big difference.  A generic
> function is a real function that selects the appropriate method
> at runtime rather than compile time.
> 
> > 2. what is exactly multiple dispatch(i tried to search a gud tutorial
> > for this, but,....)
> 
> Dispatch means the generic function decides which method to "dispatch"
> on the arguments depending on the argument types.  Multiple dispatch
> means the generic function can dispatch different methods depending
> on the types of all its arguments, not just the first one.
> 
> In C++, you think of a member function as being contained inside the
> class it's defined in.  But in reality, that's just syntactic sugar
> for calling different functions based on the type of the object that
> the function is a "member" of.  Underneath the hood, the object is
> really just the first argument of the function.
> 
> So you can think of
>    "MyObject.MyFunction(args)"
> as
>    "MyFunction(MyObject,args)",
> 
> which is a function that's overloaded based on its first argument.
> This is called "single dispatch" because it only dispatches on
> the first argument, ie. the object whose class the function is
> defined in.  CLOS generic functions dispatch on all their arguments,
> not just one, so they're an example of multiple dispatch.
> 
> > 3. Can I have member functions for any lisp class?
> 
> As you might have guessed by now, the answer is NO.  All
> class-specific operations on CLOS objects are performed by
> external methods, which are called by generic functions.
> 
> > 4. how can i achieve dynamic polymorphism using CLOS?
> 
> Dynamic polymorphism is automatic in dynamically typed languages.
> Just define a function without specifying its argument types, and
> it will be polymorphic.

In a more object-oriented sense, 'polymorphic' means
that one can send a certain message to objects and
each object will interpret the message based on its
method implementation. So different objects can
'behave' differently.

In CLOS terms, one calls a generic function with a set
of arguments. Based on the set of applicable methods
(of this generic function) and a method combination,
an effective method is computed and called with the original set
of parameters. Different sets of arguments can
lead to different effective methods being called -> Polymorphism.

Since the methods can be changed at runtime, the
method combination can be changed at runtime,
the class hierarchies can be changed at runtime ...
this is another reason why Common Lisp is a
'object-oriented dynamic' programming language
(OODL). Again this is based on the dynamic features
of the programming language and has little
to do with 'dynamic typing'.

-- 
http://lispm.dyndns.org
From: Dan Bensen
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <evejuh$8m9$1@wildfire.prairienet.org>
Rainer Joswig wrote:
> In a more object-oriented sense, 'polymorphic' means
> that one can send a certain message to objects and
> each object will interpret the message based on its
> method implementation. 

> Again this is based on the dynamic features
> of the programming language and has little
> to do with 'dynamic typing'.

You're right, that's probably what the OP meant.
I was thinking of a defun'd function that takes one or
more objects as arguments, but refers to them through
methods that are already defined.

-- 
Dan
www.prairienet.org/~dsb/
From: Pascal Bourguignon
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <87bqhwocyd.fsf@voyager.informatimago.com>
Dan Bensen <··········@cyberspace.net> writes:

> ··········@gmail.com wrote:
>> I am new to the lisp and clos. while reading clos, i have found myself
>> in deep confusion.
>
> You're not alone :)
>
> There are two recent threads on this topic here on c.l.l:
> * "Porting Ruby snippet to Lisp" started 3/17
> * "objects & namespaces"         started 2/20
>
>> i came from c++ background of oop, so i am looking
>> clos from that perspective.
>
> Beware:  CLOS is very different from C++.  A CLOS object is sort of
> like a simple struct with inheritance.  There are no member functions.
> [...]
>
>> 3. Can I have member functions for any lisp class?
>
> As you might have guessed by now, the answer is NO.  All
> class-specific operations on CLOS objects are performed by
> external methods, which are called by generic functions.

Well, right, but you can still do what you do in C/C++ in CL.

For example see this thread:

http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/bb95aa8bc0fbc294/ec31d6e96100cb18?tvc=1#ec31d6e96100cb18

-- 
__Pascal Bourguignon__
http://www.informatimago.com
http://pjb.ogamita.org
From: Rainer Joswig
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <joswig-80F24B.21493009042007@news-europe.giganews.com>
In article <························@q75g2000hsh.googlegroups.com>,
 ···········@gmail.com" <··········@gmail.com> wrote:

> Hello everybody,
> 
> I am new to the lisp and clos. while reading clos, i have found myself
> in deep confusion. i came from c++ background of oop, so i am looking
> clos from that perspective.
> 
> I have following doubts, please give me some insight:-
> 
> 1. are generic functions lisp counter part of function overloading?

Not really. Function overloading is static.
CLOS method dispatching is dynamic (decided at runtime
based on the argument classes and the available
methods).

> 2. what is exactly multiple dispatch(i tried to search a gud tutorial
> for this, but,....)

Choosing the method to call when applying a generic function
by looking at all the arguments (not only one as in
single dispatch).

> 3. Can I have member functions for any lisp class?

No. You would write a method.

> 4. how can i achieve dynamic polymorphism using CLOS?

Using generic functions and methods.

> 
> thanks in advance.
> -Tushar

-- 
http://lispm.dyndns.org
From: Wade Humeniuk
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <N6CSh.18234$hO2.13039@edtnps82>
··········@gmail.com wrote:
> Hello everybody,
> 
> I am new to the lisp and clos. while reading clos, i have found myself
> in deep confusion. i came from c++ background of oop, so i am looking
> clos from that perspective.
> 
> I have following doubts, please give me some insight:-
> 
> 1. are generic functions lisp counter part of function overloading?
> 2. what is exactly multiple dispatch(i tried to search a gud tutorial
> for this, but,....)
> 3. Can I have member functions for any lisp class?
> 4. how can i achieve dynamic polymorphism using CLOS?
> 

One insight that might help is that ....

Methods are also instances of the class standard-method.  Methods
have a separate existence from a class.  Not only that but you
can define new method classes that inherit from standard-method.
So a methods perform operations on objects (instances of classes).

http://www.lispworks.com/documentation/HyperSpec/Body/t_std_me.htm#standard-method

Wade
From: ··········@gmail.com
Subject: Re: few confusions regarding CLOS
Date: 
Message-ID: <1176187906.291319.317030@p77g2000hsh.googlegroups.com>
Thank you very much to Jon, Lous, Reiner, Dan and Wade. This
explanation has clearer my views and understanding.

thanks alot. :)

-Tushar