<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="moz-cite-prefix">On 10/02/2021 20:17, Ryan Joseph via
fpc-pascal wrote:<br>
</div>
<blockquote type="cite"
cite="mid:A49B81E8-35E6-4ABC-9D26-CBB2E96B634B@gmail.com">
<pre class="moz-quote-pre" wrap="">
</pre>
<blockquote type="cite">
<pre class="moz-quote-pre" wrap="">On Feb 10, 2021, at 11:09 AM, Ryan Joseph <a class="moz-txt-link-rfc2396E" href="mailto:genericptr@gmail.com"><genericptr@gmail.com></a> wrote:
type
TSomeTrait = trait
public
parent: TObject;
procedure DoThis;
end;
procedure TSomeTrait .DoThis;
begin
// ??? here is our issue. Is this good enough to call the TBaseClass.DoThis?
TBaseClass(parent).DoThis;
end;
</pre>
</blockquote>
<pre class="moz-quote-pre" wrap="">
Thinking about this more I don't think there's even a reason for it since Object Pascal doesn't let you do stuff like this anyways. If you want to call the super class you need to use "inherited" from within the class body. The example I posted only works if there is no virtual/override involved.
</pre>
</blockquote>
<br>
Its not about the keyword to be used.<br>
Its not even about the inheritance.<br>
<br>
In the example from
<a class="moz-txt-link-freetext" href="https://www.php.net/manual/en/language.oop5.traits.php">https://www.php.net/manual/en/language.oop5.traits.php</a><br>
The trait accesses a method defined in the class to which the trait
is applied. (It happens to be from the base class, but that does not
matter)<br>
<br>
In Pascal that would look like<br>
<br>
<font face="monospace">type<br>
TSomeTrait = trait <br>
public <br>
procedure DoTraitFoo;
<br>
end; <br>
<br>
TSomeClass = class(TObject)<br>
private<br>
trait: TSomeTrait; // whatever syntax is used so that the
trait is added<br>
public<br>
SomeVal: Integer;<br>
</font><font face="monospace"><font face="monospace"> procedure
DoSome;</font><br>
procedure Dispatch(var message); override;<br>
end;<br>
<br>
procedure TSomeTrait.DoTraitFoo;<br>
begin <br>
inherited Dispatch(nil); </font><font face="monospace"><font
face="monospace">// from TObject<br>
</font> DispatchStr(''); // from TObject<br>
DoSome; // from SomeClass<br>
SomeVal := 1; // if we can access methods, then why not data<br>
end;<br>
<br>
In the example <br>
</font><font face="monospace"><code><span style="color: #000000"><span
style="color: #0000BB"> parent</span><span style="color:
#007700">::</span><span style="color: #0000BB">sayHello</span><span
style="color: #007700">();</span></span></code><br>
</font>"parent" does not refer to the class containing the trait.
"parent" = "inherited"<br>
<br>
The php example tait's sayHello hides the inherited sayHello =>
so parent is needed.<br>
<br>
So in the example this/self of the embedded trait is the entire
object into which it is embedded.<br>
<br>
That means also: the trait and the class must be conflictfree even
for private variables. (except where maybe cross unit scoping hides
them???)<br>
<br>
-----------------<br>
Leaving scoping/conflicts aside.<br>
<br>
In Pascal TSomeTrait as it is given above can not compile, as it
does not know what DoSome,SomeVal,Dispatch are. Not until it is
embedded. <br>
Well it could compile as a generic...<br>
<br>
So, if a trait should have such access, then how to make sure the
trait can compile?<br>
Maybe:<br>
<br>
<font face="monospace"> TSomeTrait = trait <br>
imports<br>
// Those must be implement/provided by classes to which the trait
is added.<br>
</font><font face="monospace"><font face="monospace"><font
face="monospace"><font face="monospace"> procedure DoSome;</font><br>
</font> SomeVal : integer;<br>
</font> public <br>
procedure DoTraitFoo; <br>
end; <br>
</font><br>
Or (less flexible)<br>
<font face="monospace"> TSomeTrait = trait <br>
extends(TSomeClass) // can extend anything that is inherited
from TSomeClass (i.e can access anything that a TSomeClass has)<br>
</font><font face="monospace"> public <br>
procedure DoTraitFoo; <br>
end; <br>
</font>
</body>
</html>