Recent

Author Topic: self  (Read 7694 times)

anonymousstranger

  • New Member
  • *
  • Posts: 49
self
« on: September 10, 2018, 03:15:48 pm »
is it possible to assign a value to self? If so, how?

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: self
« Reply #1 on: September 10, 2018, 03:22:59 pm »
Don't do that!!! Even if it were possible (and it may well be) you'd be destroying the reference to the object itself in which you do it. As you can imagine, the consequences would be disastrous!

Why do you want to do that? There may be another (easier) way to do what you want.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: self
« Reply #2 on: September 10, 2018, 03:28:56 pm »
well I wanted to procedurally change from one reference to another in order to navigate a chain e.g.

Code: Pascal  [Select][+][-]
  1.  
  2. node=class(tobject)
  3. value:integer;
  4. fwd:node;
  5. procedure gofwd;
  6. end
  7.  
  8.  
  9. procedure node.gofwd;
  10.  
  11. begin
  12.    self:=self.fwd;
  13. end;
  14.  
  15.  

obviously, it would only be used for pointers and not instances
« Last Edit: September 10, 2018, 03:32:15 pm by anonymousstranger »

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: self
« Reply #3 on: September 10, 2018, 03:38:32 pm »
Code: Pascal  [Select][+][-]
  1. node=class(tobject)
  2.   value:integer;
  3.   fwd:node;
  4.   //procedure gofwd;
  5. end
  6.  
  7. procedure gofwd(var ANode: Node);
  8.  
  9. implementation
  10.  
  11. procedure gofwd(var ANode: Node);
  12. begin
  13.   ANode := ANode.FWD;
  14. end;
  15.  

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: self
« Reply #4 on: September 10, 2018, 03:52:16 pm »
well currently I have it implemented as a functional method e.g.

Code: Pascal  [Select][+][-]
  1.  
  2. node=class(tobject)
  3.   value:integer;
  4.   fwd:node;
  5.   function gofwd:node;
  6. end;
  7.  
  8.  
  9. function node.gofwd:node;
  10.  
  11. begin
  12.    result:=self.fwd;
  13. end;
  14.  
  15.  

just wondering if it was possible.

Bart

  • Hero Member
  • *****
  • Posts: 5721
    • Bart en Mariska's Webstek
Re: self
« Reply #5 on: September 10, 2018, 03:57:59 pm »
Why don't you just try and see if disaster strikes.
It's unlikely you will kill your compuer by doing so.
You may crash it, but even that is unlikely.

Just go for it and shoot yourself in the foot.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: self
« Reply #6 on: September 10, 2018, 04:04:41 pm »
I might add ..: in ANY compiled language. Silly idea.

Anyway: read about procedural types. That goes a long way to what you want.
Read also about TMethod..
Read also about VMT.

You can do tricks like that. I would disencourage you, though, you are not ready for that (in any language).Don't try to manipulate self itself... then you get what Bart wrote.... Try it.... I used to blow up things as well... 8-) 8-) 8-) 8-) Got even in the local newspaper blowing out windows when we were trying to make gunpowder... (true story)Obviously we succeeded: we were 11 years old...In the mid-sixties the police (one) would show up and tell us "never do that again, boys!". And we were so careful to light it in the middle of the garden and prepared the fuse (rope) with damp....) Anyway, the neighbours were not very happy. I still not know if dad paid or the insurance.To make a long story short:
If you have an idea pursue it, but you have to have a clue on how to pursue it.
« Last Edit: September 10, 2018, 04:27:08 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: self
« Reply #7 on: September 10, 2018, 04:20:04 pm »
no need to be snarky, bart  >:(
I wouldn't have these ideas if the documentation was halfway decent. I mean:

"Self Keyword

Objects have an implicit parameter, self, which can be used in method calls which can be used to qualify fields and methods of the object. The explicit use of the keyword is optional but provides clarity if desired. The following implicit and explicit use of the self qualifier for field and method access within a method are identical."

And that's it. Well, duh! But what are the limitations and restrictions? Is a simple "Self is read only." Be too much to ask?

Anyways...

Thanks Thaddy, I will look into that.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: self
« Reply #8 on: September 10, 2018, 04:22:52 pm »

Thanks Thaddy, I will look into that.
Post crossed, editted some O:-)
« Last Edit: September 10, 2018, 04:25:09 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

anonymousstranger

  • New Member
  • *
  • Posts: 49
Re: self
« Reply #9 on: September 10, 2018, 04:32:11 pm »
All I have to say is better instructions reduce (seemingly) stupid questions. And it never hurts to be nice.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: self
« Reply #10 on: September 10, 2018, 04:39:51 pm »
All I have to say is better instructions reduce (seemingly) stupid questions. And it never hurts to be nice.
Yes. Just remember self is a pointer.
Part two: If i am grumpy < >:D O:-) most will know I am trying to help despite of.. and Bart is ( he would not admit that) just as grumpy sometimes...

I might add that some people who ask questions think they know the answer. Then why ask the question?
Some people try to answer a question without the exact knowledge (including me sometimes) and some people have no clue at all. That's still an issue and isn't going to change. But when you visit the forum a lot you know the reliable answers.
« Last Edit: September 10, 2018, 04:46:53 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: self
« Reply #11 on: September 10, 2018, 05:23:01 pm »
well currently I have it implemented as a functional method e.g.

Code: Pascal  [Select][+][-]
  1. node=class(tobject)
  2.   value:integer;
  3.   fwd:node;
  4.   function gofwd:node;
  5. end;
  6.  
  7.  
  8. function node.gofwd:node;
  9. begin
  10.    result:=self.fwd;
  11. end;
  12.  
just wondering if it was possible.

Almost everything is possible ... which doesn't mean it should be done. :)
No, what you're doing now--in the code above--is just about perfect. Although I woud call it GetNextNode() or a silly thing like that  ;D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

kupferstecher

  • Hero Member
  • *****
  • Posts: 618
Re: self
« Reply #12 on: September 10, 2018, 06:21:13 pm »
well I wanted to procedurally change from one reference to another in order to navigate a chain e.g.

I still don't get the purpose. "self" is just a parameter, passed by the calling procedure, thus the scope is only local. If you call a method after changing self (don't know if possible), then its an other "self" in the called method.

Thus there would be no advantage in comparison to just using a local variable.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12345
  • Debugger - SynEdit - and more
    • wiki
Re: self
« Reply #13 on: September 10, 2018, 06:26:36 pm »
Yes, self can be assigned with a new value.
No, it will not do what the OT probably wants.

"self" acts much like a local variable. You can do "self := something".
But this will not affect the callers self.

Code: Pascal  [Select][+][-]
  1. procedure node.gofwd;
  2. begin
  3.    self:=self.fwd;
  4.   // here self is changed
  5. end;
  6. procedure node.foo;
  7. begin
  8.    self.gofwd;
  9.   // this "self" is a local var to "foo". Therefore it is NOT changed
  10. end;
  11.  

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: self
« Reply #14 on: September 10, 2018, 09:16:36 pm »
well I wanted to procedurally change from one reference to another in order to navigate a chain e.g.

I still don't get the purpose. "self" is just a parameter, passed by the calling procedure, thus the scope is only local. If you call a method after changing self (don't know if possible), then its an other "self" in the called method.

Thus there would be no advantage in comparison to just using a local variable.
self is the base pointer where the rest of the call operates on.
You amaze me in being ignorant....(unless you want be be called a noob this is totally warranted ) <Grumpy: because he (should know) knows better  >:D >:D>
 O:-) 8-) :D

Just for real Noobs:
In a class based language like object pascal, for the class methods to work it needs to know where the class and associated data actually is.
Therefor the procedures and functions of a class have a "hidden" parameter that contains a pointer to those two, actually a pointer to a record that holds a pointer to the methods record and a pointer to the data where it should operate on. This is not exclusive to Object Pascal, but e.g. C++ uses the same method.
You will understand that very easily when you think about that..,... :o :P :P :P :P
When you need to reference that pointer inside of a class, it is known as "self" like in me and I alone... :-* :'( >:D 8-) O:-)

To be cruel: Now go and reference yourself.... :-X :-X
« Last Edit: September 10, 2018, 09:27:56 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018