FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

"this" and anonymous functions

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
maustin



Joined: 15 Jun 2009
Posts: 10

PostPosted: Sun Oct 04, 2009 12:45 pm    Post subject: "this" and anonymous functions Reply with quote

Any thoughts about the "this" issue relating to anonymous functions?

function foo() {
this.field = 'name'
arr.sort(\a, b -> a[field] <=> b[field])
}

In Javascript there's a workaround like "self = this" and then use self, but it's kind of inelegant. In Smalltalk and Ruby, self is the same as the calling function and it tremendously useful.

I haven't been around in a while, but minid is still my fav language! See ya Jarret..
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Sun Oct 04, 2009 3:24 pm    Post subject: Reply with quote

Hey! Haven't seen you in a while. But it means a lot when you say it's your favorite language Very Happy

Unfortunately you have to use the same workaround in MiniD as in Javascript. Alternatively you can use bindContext, but it's still not automatic. The issue is that when the nested function is called back from whatever you call it from, the 'this' from the owning function has been lost. I'm pretty sure smalltalk and ruby manage this by having two types (?) of functions, one with a bound 'this' and one without, but I'm not entirely clear on how they're implemented, so I could be wrong.

It's something I'll keep in mind in the future though. I actually had a friend complain to me about this same issue when he was doing a lot of JS.
Back to top
View user's profile Send private message
maustin



Joined: 15 Jun 2009
Posts: 10

PostPosted: Sun Oct 04, 2009 5:36 pm    Post subject: Reply with quote

I haven't worked on it much, but here's the latest Inertia, with window texturing. I think I got ahead of myself and started doing video and networking too early. I want to rewrite it and make the core solid.

http://mike-austin.com/inertia/minid/minid-inertia.zip
http://mike-austin.com/inertia/minid/minid-inertia.png
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Mon Oct 05, 2009 12:51 am    Post subject: Reply with quote

Sweet. Well with any luck, by the time you get back into it, I'll be motivated to work on the GC. Faster GC means even better realtime performance, wee!
Back to top
View user's profile Send private message
maustin



Joined: 15 Jun 2009
Posts: 10

PostPosted: Mon Oct 05, 2009 4:32 pm    Post subject: Reply with quote

JarrettBillingsley wrote:
Hey! Haven't seen you in a while. But it means a lot when you say it's your favorite language Very Happy

Unfortunately you have to use the same workaround in MiniD as in Javascript. Alternatively you can use bindContext, but it's still not automatic. The issue is that when the nested function is called back from whatever you call it from, the 'this' from the owning function has been lost. I'm pretty sure smalltalk and ruby manage this by having two types (?) of functions, one with a bound 'this' and one without, but I'm not entirely clear on how they're implemented, so I could be wrong.

It's something I'll keep in mind in the future though. I actually had a friend complain to me about this same issue when he was doing a lot of JS.


In Smalltalk, a block acts just as though it was executed in the containing method - you can also return from the method. Ruby has both versions, which I think could be confusing to beginners.

I remember you talking about the older version of minid that supported this, but was prone to security risks? Can you refresh my memory?

Mike
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Tue Oct 06, 2009 4:59 am    Post subject: Reply with quote

Quote:
In Smalltalk, a block acts just as though it was executed in the containing method - you can also return from the method. Ruby has both versions, which I think could be confusing to beginners.


Right right, and they both do it with "magic" Wink IIRC Ruby has some kind of block stack that it uses to keep track of the owning function, so it knows how far to unwind the call stack when you do a return in a block.

And yes, you're right - I did play around with the concept in MiniD. It wasn't prone to security risks AFAIR but it was just.. icky. The language wasn't designed to have it from the start, so it felt really tacked-on. Also, not having used languages like Smalltalk and Ruby extensively, I wasn't terribly committed to the idea Wink It's something I'll reconsider later though.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group