返回“响应”对象或元组的模式的名称

时间:2019-05-06 21:06:22

标签: javascript typescript functional-programming

在编写JavaScript / TypeScript时,有时会实现一种模式,其中函数返回一个响应对象(如下所示)或一个响应“元组”(仅表示包含两个项目的数组),而不是原始值。像这样:

use std::{ env::{temp_dir, var}, fs::File, io::Read, process::Command, }; fn main() { let editor = var("EDITOR").unwrap(); let mut file_path = temp_dir(); file_path.push("editable"); File::create(&file_path).expect("Could not create file"); Command::new(editor) .arg(&file_path) .status() .expect("Something went wrong"); let mut editable = String::new(); File::open(file_path) .expect("Could not open file") .read_to_string(&mut editable); println!("File content:\n{}", editable); }

example.js

function getName() { if (userPressedOk) { return {status: "OK", name: getName()} else { return {status: "FAIL", name: ""} } }

example.ts

这是一个有点人为的示例,但这是基本思想。我试图模仿我在函数式编程语言中看到的风格。 此模式有名称吗?

1 个答案:

答案 0 :(得分:3)

我见过他们叫Result Objects

在您说从中借用的功能语言中,指定的类型通常称为/run/log/hournalResult(尽管在失败的情况下通常会提供错误消息,而不是默认值)