Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Methods tutorial on external closures

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the "method tutorial on external closure". In daily operation, I believe that many people have doubts about the method tutorial on external closure. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the method tutorial on external closure! Next, please follow the editor to study!

The first reason for this change is that it fixes the semantic vulnerability of taking the function address in the UFCS call. To this end, the proposal gives a meaning consistent with the existing semantics.

The most typical use case for an outer (closure) is to define a closure function that uses an external type context pointer (a closure function is a function that can be called a closure).

Note: this use case is associated with the label. Function. The method of switching closures is different, which creates closures from functions, but does not allow functions to use context pointers.

Suppose we create a closure function with a std.stdio.File reference as a context pointer, let's call writelnWithTime and have it write a line in the file before the current time. Typically, a function used as a closure is added to a type as a member function, but you cannot add a member function to std.stdio.File because it is defined in other libraries. One way is to create a wrapper type and define a new closure function within the wrapper, such as

Construct FileWrapper

{

File * file

Empty writelnWithTime (string msg)

{

File.writeln (Clock.currTime, "", msg)

}

}

File

Move w=FileWrapper (& file)

Move our closure = & w.writelnWithTime

It's possible, but it comes at a price. It increases the complexity of ownership semantics, potential scope errors, unnecessary runtime overhead, additional levels of indirection, and the need for additional boilerplate code to extract closures at call points. The most serious problem with this method is that it needs to increase the type of packaging. If we define this wrapper in the library used by the application, and the application needs to add its own closure function, it must create a wrapper-wrapper type to introduce another indirectness. This will not add any value to the program, but it will bring overhead and complexity.

Another method: do not be type-safe, for example:

Construct DummyType

{

Empty writelnWithTime (string msg)

{

File*file= conversion (File*) & this

File.writeln (Clock.currTime, "", msg)

}

}

File

DummyType d

Move our closure = & d.writelnWithTime

We close .ptr = convert (empty *) files

At this point, the study on the "method tutorial on external closures" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report