Calling function within class.

frame frame86 at live.com
Thu Nov 19 09:04:52 UTC 2020


On Wednesday, 18 November 2020 at 19:25:06 UTC, Vino wrote:

>   The above code is a sample code, but the logic is same, 
> correct me if my understanding is wrong, in the above code 
> "obj" is a an object for the class GetDirlist, so we are 
> accessing the class member using "obj.listFile(st)" , so why do 
> we need the "(this)" and also why is this so complicated in D 
> where as in PHP it is simple like below

You need to understand that you cannot use "this" in a function 
that you call in a new thread but task! starts a kind of new 
thread. That's not allowed by the runtime.

Your PHP code does not show any threading and PHP does not 
support threading without extensions. Also PHP does not allow 
$this for real threading extensions like parallel.

For D that means...

// before that statement you can call "this" - it points to your 
GetDirlist class.

auto fltask = task!(

    // this function is called in the new thread
    // and it's enclosed in the body of task! but could be also 
just a pointer to another
    // function. This is valid as long as the compiler do not need 
to access outside
    // variables/scope. "this" would also access the outside scope.

    function(GetDirlist obj, string st) {
             obj.listFile(st);
    }

    // end of function and thread
)

// after that statement you can call "this" again
// so we pass "this" (your object) as argument to the function 
that cannot access "this"
// and the string that the function is expecting

(this, "foo");




More information about the Digitalmars-d-learn mailing list