POV-Ray : Newsgroups : povray.programming : Overloading identifiers Server Time
29 Jul 2024 02:21:59 EDT (-0400)
  Overloading identifiers (Message 1 to 7 of 7)  
From: Nieminen Mika
Subject: Overloading identifiers
Date: 28 Jan 1999 08:49:18
Message: <36b06ade.0@news.povray.org>
I have an idea:

  In C++ you can overload functions, ie. you can specify functions with
same names but different kind or amount of arguments. For example:

int Abs(int n);
double Abs(double x);

  If you now call the function Abs() with an integer argument, it will call
the first function, while if you call it with a double argument it will call
the second.
  Why not apply this same idea to povray?
  Suppose you have, for example:

#declare Gold=<.8,.5,.2>;
#declare Gold=
  pigment
  { color Gold
  }
#declare Gold=
  texture
  { pigment { Gold }
    finish { Metal }
  }
#declare Gold=
  interior
  { (whatever would the interior of a golden object look like...)
  }
#declare Gold=
  material
  { texture { Gold }
    interior { Gold }
  }


  Now you would use 'Gold' anywhere in a material statement and it will work.
For example, if you want a gold-colored object, you would just type:

object { MyObject pigment { Gold } }

  If you want the same object as really golden:

object { MyObject texture { Gold } }

  (this would add also the metallic finish)
  Supposing that gold would be transparent, ie. it has an interior, then
you would type:

object { MyObject material { Gold } }

  If you want a gold-colored light source you would type:

light_source { <a,b,c> color Gold }


  Of course overloading #macros would be even more interesting and handy.

  I think this kind of addition will make no harm to the current version
since this is a fully backwards compatible system.
  What do you think?
  Are there any places where you can put an identifier and where it isn't
unambiguous which kind of identifier it is?

-- 
main(i){char*_="BdsyFBThhHFBThhHFRz]NFTITQF|DJIFHQhhF";while(i=
*_++)for(;i>1;printf("%s",i-70?i&1?"[]":" ":(i=0,"\n")),i/=2);} /*- Warp. -*/


Post a reply to this message

From: Ron Parker
Subject: Re: Overloading identifiers
Date: 28 Jan 1999 09:38:40
Message: <36b07670.0@news.povray.org>
On 28 Jan 1999 08:49:18 -0500, Nieminen Mika <war### [at] cctutfi> wrote:
>  Are there any places where you can put an identifier and where it isn't
>unambiguous which kind of identifier it is?

#declare A=1;
#declare A=<1,2,3>;
#declare B=2;
#declare B=<4,5,6>;
#declare C=A+B;

Should C be 3, <5,7,9>, <3,4,5>, or <5,6,7>?

But in general, why is this more useful than just putting the
type information into the declaration?

#declare cGold=rgb <whatever>
#declare pGold=pigment {color cGold}
#declare fGold=finish {whatever}
#declare tGold=texture {pigment {pGold} finish {fGold}}

You know as well as the computer does what you want to use in any 
given place, so just stick the appropriate character on:

light_source {20, cGold }
sphere {0,1 texture {tGold}}


Post a reply to this message

From: Nieminen Mika
Subject: Re: Overloading identifiers
Date: 28 Jan 1999 12:19:49
Message: <36b09c35.0@news.povray.org>
Ron Parker <par### [at] my-dejanewscom> wrote:
: #declare A=1;
: #declare A=<1,2,3>;
: #declare B=2;
: #declare B=<4,5,6>;
: #declare C=A+B;

  In this case povray could issue an error message like "Ambiguous operands".
  Then perhaps you can typecast the operands like this:

#declare C=(vector)A+(scalar)B;

  Too complicated?-)

: But in general, why is this more useful than just putting the
: type information into the declaration?

  It's more intuitive and it saves typing.
  Even if overloadable identifiers were not good, overloadable macros should
be very handy.

-- 
main(i){char*_="BdsyFBThhHFBThhHFRz]NFTITQF|DJIFHQhhF";while(i=
*_++)for(;i>1;printf("%s",i-70?i&1?"[]":" ":(i=0,"\n")),i/=2);} /*- Warp. -*/


Post a reply to this message

From: Rudy Velthuis
Subject: Re: Overloading identifiers
Date: 28 Jan 1999 12:37:21
Message: <36b0a051.0@news.povray.org>
Nieminen Mika schrieb in Nachricht <36b09c35.0@news.povray.org>...
>Ron Parker <par### [at] my-dejanewscom> wrote:
>: #declare A=1;
>: #declare A=<1,2,3>;
>: #declare B=2;
>: #declare B=<4,5,6>;
>: #declare C=A+B;
>
>  In this case povray could issue an error message like "Ambiguous
operands".
>  Then perhaps you can typecast the operands like this:
>
>#declare C=(vector)A+(scalar)B;
>
>  Too complicated?-)

Yes!

Well, not for me and you, but propably for most POV-Ray users. Remember,
that POV has a scripting language which is complicated enough for most
people already. You and I know about typecasts and functions etc. Don't
forget that even #macros are just that, and not fully fledged functions. And
in fact #declares are very similar to C/C++ #defines.

Anyway I don't see the big advantage of overloading. I don't see the
advantage of overcomplicating the script language either. Not everyone who
can use POV should be able to program.

--
Rudy Velthuis


Post a reply to this message

From: Ron Parker
Subject: Re: Overloading identifiers
Date: 28 Jan 1999 13:05:27
Message: <36b0a6e7.0@news.povray.org>
On 28 Jan 1999 12:19:49 -0500, Nieminen Mika <war### [at] cctutfi> wrote:
>Ron Parker <par### [at] my-dejanewscom> wrote:
>  In this case povray could issue an error message like "Ambiguous operands".
>  Then perhaps you can typecast the operands like this:

I can see this being confusing if one third-party .inc file does

#declare tmp=<2,3,4>;

and another third-party .inc file does

#declare tmp=5;
sphere {tmp, 2 texture {blah}};

So is this ambiguous, which would be confusing, or does it use the
<2,3,4> definition of tmp, which would just be wrong? Granted, they 
should have used #local, but you can't count on that.  Except for 
floats and vectors, though, I guess I don't see any technical problems
with the idea.  I'm just not sure it's strictly necessary and it seems
like it would just add to the confusion newbies already experience.

>  Even if overloadable identifiers were not good, overloadable macros should
>be very handy.

This I tend to agree with, but you can still accomplish the same thing by
using a good naming scheme.  Also, I'm thinking you would have to have
a different number of arguments to each version of the macro, as there's 
no other way to tell them apart.  The most likely usage of this kind of
macro overloading seems like it would be to default parameters, and it 
seems like that would be better accomplished by directly allowing default 
parameters.  (For example, I wish my TorusSpline macro could have the 
third parameter be optional.)  This wouldn't even require any syntax 
changes, just a parser change: if it finds a ')' when it expected to find 
a comma or another parameter, it should just set the rest of the formal 
parameters to undefined and call the macro rather than generating an 
error as it does now.  This will also require a tokenizer change to stop 
the search through the stack of definitions when an identifier is marked 
as "temporarily undefined" at some level, to prevent hard-to-find problems 
with code like this, which I would expect to generate a sphere of radius 1 
centered on <2,2,2>:

#macro baz(foo, bar)
  #ifndef (bar)
    #declare bar=1;
  #end
  sphere {foo,bar}
#end

#declare bar=5;

baz(2);


Post a reply to this message

From: Nieminen Mika
Subject: Re: Overloading identifiers
Date: 28 Jan 1999 13:36:23
Message: <36b0ae27.0@news.povray.org>
Ron Parker <par### [at] my-dejanewscom> wrote:
: The most likely usage of this kind of
: macro overloading seems like it would be to default parameters, and it 
: seems like that would be better accomplished by directly allowing default 
: parameters.
(snip)
: #macro baz(foo, bar)
:   #ifndef (bar)
:     #declare bar=1;
:   #end
:   sphere {foo,bar}
: #end

  That's a very good point.
  I threw the idea on overloadable identifiers just as a suggestion but this
default parameter system is IMHO a very worthy of consideration.
  Perhaps it could go a little more further so that you could type:

#macro Test(a,b,c) ... #end
...

Test(1,2)
Test(1,,3)
Test(,2,3)

-- 
main(i){char*_="BdsyFBThhHFBThhHFRz]NFTITQF|DJIFHQhhF";while(i=
*_++)for(;i>1;printf("%s",i-70?i&1?"[]":" ":(i=0,"\n")),i/=2);} /*- Warp. -*/


Post a reply to this message

From: Jim Kress
Subject: Re: Overloading identifiers
Date: 29 Jan 1999 18:00:38
Message: <36b23d96.0@news.povray.org>
It's NOT more intuitive!  It's more confusing.  While some may like
'overloading' in C++ (where the heck did they get that name?).  It tends to
make the code and descriptions of the code (to some poor schmuck like me who
doesn't know how to code in C++) very confusing.

Ron's suggestion is more in line with 30 years of coding history and, to me
at least, much more clear.

--
Jim

Check out my web site http://www.kressworks.com/
It'll blow your mind (politically), stimulate your senses (artistically)
and provide scientific insights beyond compare!

Be sure to read the Warp maintained POV VFAQ:
http://iki.fi/warp/povVFAQ.html




Nieminen Mika wrote in message <36b09c35.0@news.povray.org>...
>Ron Parker <par### [at] my-dejanewscom> wrote:
>: #declare A=1;
>: #declare A=<1,2,3>;
>: #declare B=2;
>: #declare B=<4,5,6>;
>: #declare C=A+B;
>
>  In this case povray could issue an error message like "Ambiguous
operands".
>  Then perhaps you can typecast the operands like this:
>
>#declare C=(vector)A+(scalar)B;
>
>  Too complicated?-)
>
>: But in general, why is this more useful than just putting the
>: type information into the declaration?
>
>  It's more intuitive and it saves typing.
>  Even if overloadable identifiers were not good, overloadable macros
should
>be very handy.
>
>--
>main(i){char*_="BdsyFBThhHFBThhHFRz]NFTITQF|DJIFHQhhF";while(i=
>*_++)for(;i>1;printf("%s",i-70?i&1?"[]":" ":(i=0,"\n")),i/=2);} /*-
Warp. -*/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.