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

How to write code to implement external closures

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

Share

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

This article introduces the relevant knowledge of "how to write code to implement external closures". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

At the function definition point:

Functions need to be defined within the virtual wrapper type

You need to convert from a pseudo-type context pointer to the actual type you need

This discards all type safety of the context pointer type and results in runtime overhead

At the function call point:

You need to declare an instance of a virtual type; note that this declaration serves no purpose other than getting a closure for the function.

After the closure is extracted, set ptr to a separate statement that creates a new location for run-time errors

To set the closure context pointer, the type must be cast to (null *); like definition points, this discards any type safety of the context pointer type and results in some runtime overhead.

The outer (closure) solution is simple, clear, and type-safe. There are no pseudo-wrapper types, no conversions at definition or call points, no runtime overhead, and closures are extracted in type-safe statements.

Outer (closure) empty writelnWithTime (ref File file,string msg)

{

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

}

File

Move our closure = & file.writelnWithTime

Major change / deprecation process

The proposal only requires the use of a new syntax that will not break d.

Examples

= =

An example of demonstrating usage:

Construct Foo

{

Empty bus (int x)

{

}

}

Outer (closure) empty baz (ref Foo foo,int x)

{

}

/ / Note that even if the `bar` member function is defined in the `foostructure'

The / / `baz` function is the same as `bar`ABI.

Empty closure (int) dg

Foo foo

Dg=&foo.bar; / / ordinary member function closure

Dg (42); / / call foo.bar (42)

Dg=&foo.baz; / / use UFCS to extract the closure of the `outer (closure) `baz function

Dg (42); / / call baz (foo,42)

Dg=&baz;// error: cannot implicitly convert an expression (baz) of type ref Foo,int to `empty closure (int) `outside (closure)`

More realistic examples:

Import std.stdio,std.datetime

Outer (closure) empty writelnWithTime (T) (ref File foo,T [] buffer)

{

Foo.writeln (Clock.currTime, "", buffer)

}

Empty main ()

{

DumpInfo (& stdout.writeln! (const (char) [])

/ / use UFCS to get closures to the `outer (closure) `writelnWithTime function

DumpInfo (& stdout.writelnWithTime! (const (char) []))

}

Empty dumpInfo (empty closure (const (char) []) writer)

{

Writer ("one Info:")

Writer ("2 = 2")

Writer ("3 seconds = 4")

}

This is the end of "how to write code to implement the next part of the external closure". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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