Flow function类

flow用来对js进行静态类型检查,检查分为:
类型注释: 事先注释好我们期待的类型, Flow 会基于这些注释来判断
类型推断: 通过变量的使用上下文来推断出变量类型, 然后根据这些推断来检查类型

function函数类有两个地方用到了类型,参数输入的时候和return返回数据的时候,下面规定了参数必须是string类型,返回值也是string类型,所以后面一条报错。

// @flow
function concat(a: string, b: string): string {
  return a + b;
}

concat("foo", "bar"); // Works!
// $ExpectError
concat(true, false);  // Error!

当省略注释的时候,就使用类型推断。

// @flow
function concat(a, b) {
  return a + b;//+ 可以是数字类型的相加,也可以是字符串连接
}

concat("foo", "bar"); // Works!
concat(1, 2);         // Works!

函数的参数使用注释的情况:

1、函数声明

function method(str, bool, ...nums) {//省略注释
  // ...
}

function method(str: string, bool?: boolean, ...nums: Array<number>): void {//有注释的
  // ...
}

 2、箭头函数

let method = (str, bool, ...nums) => {//无注释
  // ...
};

let method = (str: string, bool?: boolean, ...nums: Array<number>): void => {//无注释
  // ...
};

str: string--正常带注释写法
bool?: boolean,--该参数为可选的,用?标记,能接受省略undefined,或者其他匹配的值(这里为boolean类型),但是不接受null

bool: ?boolean,表示可以为boolean或者null。:?T表示可以是null,或者规定的Type

// @flow
function method(optionalValue?: string) {
  // ...
}

method();          // Works.省略
method(undefined); // Works.undefined
method("string");  // Works.规定的类型
// $ExpectError
method(null);      // Error!

...nums: Array<number>--剩余参数,类型为Array,每一项都为number类型,实际意思就是,传入的参数得是number类型的,所有的参数组成的这个剩余参数就变成了一个值number的数组。

// @flow
function method(...args: Array<number>) {
  // ...
}

method();        // Works.
method(1);       // Works.
method(1, 2);    // Works.
method(1, 2, 3); // Works.

异步(async)函数需要返回一个promise

// @flow
async function method(): Promise<number> {
  return 123;
}

this返回的是函数调用call的那个上下文 

function method() {
  return this;
}

var num: number = method.call(42);
// $ExpectError
var str: string = method.call(42);

 predict function 推断函数?

function truthy(a, b): boolean {
  return a && b; // a为真,返回b,a为假,返回a,所以返回值不一定是Boolean
}

function concat(a: ?string, b: ?string): string {
  if (truthy(a, b)) {
    // $ExpectError
    return a + b;
  }
  return '';
}

关于%check的使用,没看懂

function isString(y): %checks {
  return typeof y === "string";
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Stateflow中调用function函数,可以使用Stateflow中的Function-Call Action来实现。具体步骤如下: 1. 在Stateflow中创建一个State或者Subchart。 2. 在State或者Subchart中添加一个Function-Call Action。 3. 在Function-Call Action中选择要调用的function函数。 4. 在Function-Call Action中设置输入参数和输出参数。 5. 在Stateflow中添加一个Transition,将State或者Subchart与下一个State或者Subchart连接起来。 6. 在Transition中设置Transition Action,将Function-Call Action放入Transition Action中。 7. 运行Stateflow模型,当State或者Subchart进入Transition时,Function-Call Action会自动调用function函数,并将输入参数传递给function函数,同时将输出参数传递给Transition Action。 注意:在Stateflow中调用function函数时,需要确保function函数已经被定义并且可用。同时,需要注意输入参数和输出参数的型和数量要与function函数的定义相匹配。 ### 回答2: Stateflow是一种建模工具,它可以模拟系统的状态转移和事件响应。在Stateflow中,可以使用多种方法来实现功能。其中一个方法是使用Function函数,可以将一段具有独立功能的代码封装成一个函数,让Stateflow中的状态图调用。 使用Stateflow调用Function函数的步骤如下: 首先,在Stateflow中创建一个function函数。可以在模型中右键单击选择「Add Function」,或者在Stateflow Editor的Function库中选择要使用的函数: 其次,在要使用该函数的状态中,使用「Action」和「Function」组合框。 最后,在「Function」组合框中选择之前创建的function函数,将其添加到状态行为中即可。 在Stateflow中调用Function函数的好处是实现了代码复用,可以避免代码的重复编写和错误。此外,这种方法与其他方法相比,代码更易读、更易维护,也让代码更具有可移植性。 需要注意的是,在Stateflow中调用Function函数时,函数的返回值型和参数型应该与调用函数的状态或过渡的输入/输出相匹配。 总之,使用Stateflow调用Function函数是一个非常实用和高效的方法,在设计复杂系统模型时可以减少工作量,提高效率和可维护性。 ### 回答3: 在使用Stateflow时,可以使用Function函数对模型中的代码进行封装和重用。通过使用Function函数,我们可以将多个State或Transition中共同执行的代码放在一个Function中,并且让这些State或Transition直接调用Function。这样可以减少模型的复杂性,提高代码的可读性和可维护性。 在Stateflow中,可以使用以下步骤进行Function函数的调用: 1. 创建Function:在模型文件中,使用Function图标创建一个Function并编写需要执行的代码。 2. 在需要调用Function的State或Transition中添加Function Caller:通过将Function Caller组件拖放到需要调用Function的State或Transition中,从而将Function与State或Transition关联起来。 3. 配置Function Caller:在Function Caller的参数配置界面中,选择需要调用的Function。在这里,可以将输入参数、输出参数以及调用时机等信息进行配置。 4. 在State或Transition的Action中调用Function Caller:在State或Transition的Action中,使用Function Caller的名称调用Function。在执行Action时,Function Caller将自动调用Function并传递输入参数。调用完成后,如果Function有输出参数,则可以使用Function Caller中对应的输出变量进行访问。 总的来说,使用Stateflow调用Function函数不仅可以提高模型的效率和可读性,还可以减少代码重复。但是,在使用Function函数时,需要注意变量名、型和作用域等问题,以确保程序的正确性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值