内容简介:When I was atSerialisation is an obvious use-case and almost always the first one anyone comes up with if pressed for an example. But there’s a lot more, likeLet’s say one wants to write a
When I was at CppCon 2016 I overheard someone ask “Everyone keeps talking about reflection, but why do we actually need it?”. A few years before that, I also would have had difficulty understanding why it would be useful. After years of writing D, it’s hard to imagine life without it.
Serialisation is an obvious use-case and almost always the first one anyone comes up with if pressed for an example. But there’s a lot more, like Design by Instrospection . It allows one to write a mocking framework . You start seeing applications everywhere. My favourite way to use it is to make the compiler write code for me.
Let’s say one wants to write a Python extension in native code . Each top-level Python function must have a corresponding C function (well, C ABI at least) that looks something like this:
PyObject* myfunc(PyObject* self, PyObject* args, PyObject* kwargs) { // ... return result; }
There are a lot of details to take care of. There’s error handling , managing ref counts, and in all likelihood conversion from and to Python types since in most cases one is usually interested in calling existing pre-written code and make it available to Python. It’s tedious, and I haven’t even shown all the boilerplate to initialise the Python module and register the functions. The code for two simple functions ends up looking like this . Just thinking of clicking that link makes me sigh. Imagine what making calls into a real codebase would look like. We can do better:
import autowrap; mixin( wrapDlang!( LibraryName("mylib"), Modules( Module("mymodule"), Module("myothermodule"), ) ) );
The code above, when compiled, will generate a Python extension (shared library) that exposes every D function marked as “export” in the modules “mymodule” and “myothermodule” as Python functions. It’ll even convert their names from camelCase to snake_case. Any D exceptions thrown will become Python exceptions. D structs and classes become Python classses. If the original D functions take a D string, you’ll be able to pass Python strings to them in user code. Modulo bugs, this… works ! The code shown above is the only code that needs to be written. Setting up the build system takes more work!
“Only” two D features are used here: the ability to do reflection at compile-time (and therefore to know which functions are in those modules and what types they take and return), and being able to mix in strings at compile-time. All the boilerplate is written for the user and inserted inline as if written by hand, but it’s the compiler that’s doing the heavy lifting.
Imagine now that your boss, pleased with these results, now wants you to also make the same D code avaiable to Excel users. The code changes not one bit, those lines above also work for Excel (the trick is telling the build system to depend on the autowrap:excel dub package instead of autowrap:python). Instead of snake_case functions, one now gets PascalCase as per Excel convention.
Same API, same functionality, different implementation. And no code to write for the user. The curious can see how it’s done on github .
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入浅出MFC (第二版)
侯俊杰 / 华中科技大学出版社 / 2001-1 / 80.00元
《深入浅出MFC》分为四大篇。第一篇提出学习MFC程序设计之前的必要基础,包括Widnows程序的基本观念以及C++的高阶议题。“学前基础”是相当主观的认定,但作者是甚于自己的学习经验以及教学经验,其挑选应该颇具说服力。第二篇介绍Visual C++整合环境开发工具。此篇只是提纲挈领,并不企图取代Visual C++使用手册;然而对于软件使用的老手,此篇或已足以帮助掌握Visual C++整合环境......一起来看看 《深入浅出MFC (第二版)》 这本书的介绍吧!