Hide "this"

Request a new command, feature, or improvement

Moderator: MSandro

Post Reply
SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Hide "this"

Post by SimpleSi » Jul 5th, '17, 08:10

Can I start off by saying that I'm NO CS expert and I work(ed) in UK primary education using Scratch.

The Scratch concept of low floor-high ceiling has always been a good mantra for me :)

So - I on my first real foray into using GP - I looked at RabbitsAndGrass and can see its a good example of defining classes and then creating instances from the classes.

However, I'm struggling to see how sprinking the code with "this" does anything except add unnecessary clutter.

I can't find anything in the code that uses "this" but it seems to be needed in a lot of user defined blocks.

Now if there is a very good reason for needing "this" for bigger projects - how about just using the concept of the black right arrow (e.g like wait block) to hide "this"

Like I say, I'm no CS expert but I make a good (hopefully constructive) CS critic :)

Simon

PS
I'm against "self" in Python as well for same reason - my attitude is that the interpreter should be able to determine scope of functions without the programmer having to tell it every 5 seconds :)

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Hide "this"

Post by JohnM » Jul 5th, '17, 10:58

"This" issue (pun intended!) has been emerging as a source of awkwardness and confusion.

Here's the source of the problem: Most of the time, you define a method to help with some internal operation, such as drawing. In that case, the method is called from another method in the same instance, and it seems pointless to have a "this" parameter in the header and to be required to pass it in every call. However, sometimes a method is meant to be called by another object: either from a different class or from a different instance of the same class. In that case, you need some way to specify the object you are talking to. Some versions of Logo had multiple turtles and used an optional prefix to specify the turtle for which a command was intended. For example:

Code: Select all

  t1, move 10 -- make turtle 1 move 10 steps forward
  t2, move -10 -- make turtle 2 move 10 steps backward
  move 10 -- might mean both turtles (StarLogo did that) or the last turtle addressed
It would be nice to have a solution for GP where the extra "this" parameter was hidden until you actually needed it. The team has talked about various ways to represent and implement that idea and I'm hoping we'll get a chance to try one of them in the coming year.

Thanks for mentioning "this". :-)

-- John

Post Reply