discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

how to document $this on the Objects Page?

V
vulcan_@mac.com
Fri, Aug 8, 2025 2:41 PM

i know what this is for .. lots of work in the objects of C++ and Java,

But $this in a functional language has to be more than a little funky

Could the group please brief me on PKriens PR so that i can update the page on Objects?

i know what `this` is for .. lots of work in the objects of C++ and Java, But $this in a functional language has to be more than a little funky Could the group please [brief me on PKriens PR](https://github.com/openscad/openscad/pull/6022) so that i can update the page on Objects?
AM
Adrian Mariano
Fri, Aug 8, 2025 3:07 PM

Isn’t it premature to worry about documenting stuff hadn’t even made it
into dev yet?

On Fri, Aug 8, 2025 at 10:42 vulcan_--- via Discuss <
discuss@lists.openscad.org> wrote:

i know what this is for .. lots of work in the objects of C++ and Java,

But $this in a functional language has to be more than a little funky

Could the group please brief me on PKriens PR
https://github.com/openscad/openscad/pull/6022 so that i can update the
page on Objects?


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Isn’t it premature to worry about documenting stuff hadn’t even made it into dev yet? On Fri, Aug 8, 2025 at 10:42 vulcan_--- via Discuss < discuss@lists.openscad.org> wrote: > i know what this is for .. lots of work in the objects of C++ and Java, > > But $this in a functional language has to be more than a little funky > > Could the group please brief me on PKriens PR > <https://github.com/openscad/openscad/pull/6022> so that i can update the > page on Objects? > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
JB
Jordan Brown
Fri, Aug 8, 2025 3:33 PM

On 8/8/2025 7:41 AM, vulcan_--- via Discuss wrote:

i know what |this| is for .. lots of work in the objects of C++ and Java,

But $this in a functional language has to be more than a little funky

Could the group please brief me on PKriens PR
https://github.com/openscad/openscad/pull/6022 so that i can update
the page on Objects?

Updating the docs is premature, since the feature isn't in yet.

The essence is the same as it is in JavaScript.

If you have an object o with a member f that is a function, and you
invoke the function as o.f(...), the function is provided with a special
variable $this that is the object.  (Or that is a copy of the object;
you can't tell.)

This allows you to do things like

function rect(size) = object(
     size = size,
     area = function() $this.size.x * $this.size.y
);
r = rect([10,20]);
echo(r.area());
On 8/8/2025 7:41 AM, vulcan_--- via Discuss wrote: > > i know what |this| is for .. lots of work in the objects of C++ and Java, > > But $this in a functional language has to be more than a little funky > > Could the group please brief me on PKriens PR > <https://github.com/openscad/openscad/pull/6022> so that i can update > the page on Objects? > Updating the docs is premature, since the feature isn't in yet. The essence is the same as it is in JavaScript. If you have an object o with a member f that is a function, and you invoke the function as o.f(...), the function is provided with a special variable $this that is the object.  (Or that is a copy of the object; you can't tell.) This allows you to do things like function rect(size) = object( size = size, area = function() $this.size.x * $this.size.y ); r = rect([10,20]); echo(r.area());
V
vulcan_@mac.com
Sat, Aug 9, 2025 12:10 AM

Jordan Brown wrote:

On 8/8/2025 7:41 AM, vulcan_--- via Discuss wrote:

i know what |this| is for .. lots of work in the objects of C++ and Java,

Updating the docs is premature, since the feature isn't in yet.

all i needed to read was that it is premature to document it .. mind you, i did just see the PR pass all tests so ..

all done then, and no action to take

Jordan Brown wrote: > On 8/8/2025 7:41 AM, vulcan_--- via Discuss wrote: > > > i know what |this| is for .. lots of work in the objects of C++ and Java, > > Updating the docs is premature, since the feature isn't in yet. all i needed to read was that it is premature to document it .. mind you, i did just see the PR pass all tests so .. all done then, and no action to take
JB
Jordan Brown
Sat, Aug 9, 2025 12:40 AM

On 8/8/2025 5:10 PM, vulcan_--- via Discuss wrote:

mind you, i did just see the PR pass all tests so ..

Doesn't mean that it's in its final form. I've had several PRs pass all
of the tests and then die, or change radically, or go on hold for a long
time.

On 8/8/2025 5:10 PM, vulcan_--- via Discuss wrote: > > mind you, i did just see the PR pass all tests so .. > Doesn't mean that it's in its final form. I've had several PRs pass all of the tests and then die, or change radically, or go on hold for a long time.
PK
Peter Kriens
Sat, Aug 9, 2025 2:36 PM

I move it out of draft and hope it gets accepted soon!

Basically a function in an object will know where it came from. The object it came from will be $this. Additionally, it can access all fields in the objects without this prefix.

o = object( 
	a	=	42, 
	f1	=	function() a, 
	f2	=	function() $this.a );

assert( o.f1() == 42 );
assert( o.f2() == 42 );

The context of a method (a function from an object) is:

parent context
fields from objects + $this
parameters
local variables

The lookup is for bottom to top. So a parameter that has the same name as a field will have priority. A local variable overrides the parameter and the object field. You can always access the field with the $this variable.

Functions remain bound tot the object unless assigned to another object.

f1 = o.f1;
assert( f1() == 42 );
f = object( a=4711, f=f1 ).f;
assert( f() == 4711 );

Functions have access to themselves so you can make recursive functions like in a Y combinator:

Y = object( f=function(n) n > 1 ? n*f(n-1) : 1).f; 
assert(Y(5)==120);

This will allow for objects. For example,

solid  = Prism( size=[10,10,10], size2=5);
inside = box.inside(wall=4);

As an old OO guy I think it might be useful to establish some conventions around some meta data for these objects (like a class name) but that has no urgency. Maybe best to wait a bit with this until we have more experience.

I hope the PR will soon be accepted. I am not unhappy with the current solution.

Peter Kriens

On 9 Aug 2025, at 02:40, Jordan Brown via Discuss discuss@lists.openscad.org wrote:

On 8/8/2025 5:10 PM, vulcan_--- via Discuss wrote:

mind you, i did just see the PR pass all tests so ..

Doesn't mean that it's in its final form.  I've had several PRs pass all of the tests and then die, or change radically, or go on hold for a long time.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I move it out of draft and hope it gets accepted soon! Basically a function in an object will know where it came from. The object it came from will be $this. Additionally, it can access all fields in the objects without this prefix. o = object( a = 42, f1 = function() a, f2 = function() $this.a ); assert( o.f1() == 42 ); assert( o.f2() == 42 ); The context of a method (a function from an object) is: parent context fields from objects + $this parameters local variables The lookup is for bottom to top. So a parameter that has the same name as a field will have priority. A local variable overrides the parameter and the object field. You can always access the field with the $this variable. Functions remain bound tot the object unless assigned to another object. f1 = o.f1; assert( f1() == 42 ); f = object( a=4711, f=f1 ).f; assert( f() == 4711 ); Functions have access to themselves so you can make recursive functions like in a Y combinator: Y = object( f=function(n) n > 1 ? n*f(n-1) : 1).f; assert(Y(5)==120); This will allow for objects. For example, solid = Prism( size=[10,10,10], size2=5); inside = box.inside(wall=4); As an old OO guy I think it might be useful to establish some conventions around some meta data for these objects (like a class name) but that has no urgency. Maybe best to wait a bit with this until we have more experience. I hope the PR will soon be accepted. I am not unhappy with the current solution. Peter Kriens > On 9 Aug 2025, at 02:40, Jordan Brown via Discuss <discuss@lists.openscad.org> wrote: > > On 8/8/2025 5:10 PM, vulcan_--- via Discuss wrote: >> mind you, i did just see the PR pass all tests so .. >> > Doesn't mean that it's in its final form. I've had several PRs pass all of the tests and then die, or change radically, or go on hold for a long time. > > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org