|Pliant talk forum
Discussion: On methods and formal parameters
|Message posted by maybe Marcus on 2002/05/16 11:55:24
|On the last messages posted regarding error messages, I've noticed you using the
term 'class' to refer to the parameter that comes before the name of the method,
and 'parameters' to the ones that come after. Viz.,
> . the type of the class 'p' is missing.
> . the method has an argument 'r' with type 'Int'
Is there any specific reason to call the first parameter a 'class'?
Moreover, Patrice does not refer to the first parameter as a parameter, viz.,
> method p -> r # method '->' with one parameter
One would say that method '->' in that example has two formal parameters, p and r.
What would be the best way to justify the existence of this syntactical distin-
ction between functions and methods (a simple example would be enlightening).
|Message posted by maybe Hubert Tonneau on 2002/05/16 12:12:47
|The only difference between the first argument (sometime called 'class') and
the other ones under Pliant is that the first and only the first one will be
used to select the right generic method or funtion at run time.
In other words, there is no 'class' notion in Pliant, as opposed to object
programming, except that the first parameter type is used by generic methods
to select the right one at run time as opposed to compile time.
> What would be the best way to justify the existence of this syntactical distin-
> ction between functions and methods (a simple example would be enlightening).
It's just about making the program easy to read.
On the semantic side, the rule should be the following: if the function is
extracting some part of the data, it should be implemented as a method.
If it's doing computations from the data returning something that is not
a part (maybe virtual part) of the data, it should be a plain function.
In other words, a method should basically be something that computes the value
of a virtual field in a data.
|Message posted by maybe Marcus on 2002/05/20 01:03:16
|I was studying the 'Generic Methods' documentation trying to grasp the idea
of Pliant's generic methods/functions.
I didn't quite understand that example (and the usage of generic methods).
1- what is the meaning of the 'generic' directive inside the first
2- Could you provide another example to illustrate the point?
|Message posted by marcus on 2002/05/20 01:21:52
|>The only difference between the first argument (sometime called 'class') and
>the other ones under Pliant is that the first and only the first one will be
>used to select the right generic method or funtion at run time.
Do you mean that the first parameter of a function is also used to determined
to select a generic method?
|Message posted by maybe Hubert Tonneau on 2002/05/20 12:58:01
|> Do you mean that the first parameter of a function is also used to determined
> to select a generic method?
Yes, when you call a generic method, the first argument has to be a true
Pliant object. Any true Pliant object has a header containing it's references
counter and a pointer to it's real type.
The pointer to it's real type is used to get the right fonction at run time
because any generic method is in facts an index in the type functions array.
|Message posted by maybe Hubert Tonneau on 2002/05/20 13:03:43
|> I was studying the 'Generic Methods' documentation trying to grasp the idea
> of Pliant's generic methods/functions.
It's just an efficient way to select the right function or method at run time
rather than at compile time.
In other words, it's basically the 'driver' notion: you have several possible
instances (drivers) (implemented as types in Pliant) that look basically the same
(have the same set of generic methods) and you are witting peaces of code that
will work on any of these instances since it's only relying on the API (the
generic methods) and is not concerned with the low level details.
|Message posted by marcus on 2002/05/20 13:48:39
|Two quickies, to close the issue:
1- had I implemented versions of the 'show' function for
handling different data types, and called them passing as a parameter an
ordinary Pliant object (as opposed to true one), what would have been the
difference, as far as compilation and efficiency are concerned?
I mean, instead of the code in pliant/language/compiler/function/generic.html
method i show -> s
arg Int i ; arg Str s
s := string i
method i show -> s
arg Float i ; arg Str s
s := string 'cas Int':i
gvar Int i
i := 5
2- Could you give me a reference to existing Pliant code which uses the generic
|Message posted by maybe Hubert Tonneau on 2002/05/20 16:52:58
|> gvar Int i
Any global variable is a true Pliant object, as opposed to local variables that
If you are using a generic method not on a true Pliant object, it is high
likely that you will get an immediat crash.
A good example of generic methods implementing a kind of 'driver' system is
'StreamDriver' data type in module /pliant/language/stream/stream.pli
defining the API,
then 'NativeStreamDriver' in /pliant/language/stream/native.pli,
'TcpStreamDriver' in /pliant/language/stream/tcp.pli,
'CompressZlibStreamDriver' in /pliant/language/stream/zlib.pli
'SerialStreamDriver' in /pliant/language/stream/serial.pli