如何使用 typescript 的 AST api 和打印机创建带有文档注释的函数?
/**
* foo function
*/
function foo () {}
Run Code Online (Sandbox Code Playgroud)
以下代码生成该函数。
function foo () {}
Run Code Online (Sandbox Code Playgroud)
import ts from 'typescript';
const fooFunction = ts.createFunctionDeclaration(
undefined,
undefined,
undefined,
ts.createIdentifier("foo"),
undefined,
[],
undefined,
ts.createBlock(
[],
false
)
)
const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
});
const resultFile = ts.createSourceFile(
"example.ts",
"",
ts.ScriptTarget.Latest,
/*setParentNodes*/ false,
ts.ScriptKind.TS
);
const result = printer.printNode(
ts.EmitHint.Unspecified,
fooFunction,
resultFile
);
console.log(result);
Run Code Online (Sandbox Code Playgroud) 下面的代码可以正常工作,但我想将ostream&operator <<移到类declearation之外,就像我使用hash :: operator []一样.
#include<iostream>
#include<map>
using namespace std;
template <class T>
class hash {
private:
map<string, T> _map;
public:
T& operator[] (string x);
friend ostream& operator<<(ostream& out, const hash<T> &rhs) { return out << "test"; }
};
template <class T>
T & hash<T>::operator[](string x) {
return _map[x];
}
int main () {
hash<int> myobject;
myobject["a"] = 1;
cout << myobject["a"] << endl;
cout << myobject << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
template <class T>
ostream& operator<<(...) {
return …Run Code Online (Sandbox Code Playgroud) 是否有类似ActionController::Base#skip_before_filterActiveRecord回调的方法after_create?我有一些类的行为类似,为了减少代码重复,我创建了一个抽象类。抽象包含关系信息,验证和after_create创建新消息的回调。Message实现此抽象,因此保存消息会导致堆栈溢出。
我可以从模型中删除回调吗?
class Parent < ActiveRecord::Base
self.abstract_class = true
after_create :notify
def notify
Message.create
end
end
class Message < Parent
# skip after_create :notify
end
class Child < Parent
end
Run Code Online (Sandbox Code Playgroud) 在某些情况下,存储字段是有意义的。例如,如果您有一个包含标题、日期和非常大的内容字段的文档,您可能只想检索标题和日期,而不必从大型 _source 字段中提取这些字段
stored_fields 参数是关于显式标记为存储在映射中的字段,默认情况下关闭,通常不建议这样做。使用源过滤来选择要返回的原始源文档的子集。
默认情况下,所有支持文档值的字段都启用它们。
我有包含title(短字符串)和content(> 1MB)的文档。我想搜索匹配的标题,并返回标题。
GET /_search
{ _source: "obj.title", ... }
Run Code Online (Sandbox Code Playgroud)
GET /_search
{ _source: false, stored_fields: ["title"], ... }
Run Code Online (Sandbox Code Playgroud)
GET /_search
{_source: false, stored_fields: "_none_", docvalue_fields: "title", ... }
Run Code Online (Sandbox Code Playgroud)
可以,然后呢
_source从磁盘读取完整的标题和内容,然后应用过滤器并仅返回标题,或者elasticsearch只会从磁盘读取标题?我正在为嵌入式项目编写Rust,而我的main函数签名是
#[entry]
fn main() -> !
Run Code Online (Sandbox Code Playgroud)
我知道这意味着它永远不会返回,而且我通常在main的末尾进入无限循环。
我想?在主要功能中使用try运算符,但无法在文档中搜索rust ? in !。我该如何用单词拼写出来?
我可以?在() -> !功能中使用吗?