F#教程:向函数传入Record类型


我们继续Record的学习。我们试着定义并调用一个传入Vector类型参数的magnify函数:

type Vector = { x: int; y: int} 
let    magnify v n = { x = v.x * n; y = v.y * n}
let v = { x = 10; y = 15 } 
printfn "%A" (magnify v 2)

  但是,如果是如下代码就会出现编译错误:

type Vector = { x: int; y: int} 
type Point = { x: float; y: float}     //    新增行 
let    magnify v n = { x = v.x * n; y = v.y * n}
let v : Vector = { x = 10; y = 15 }     // 显式定义为Vector类型 
printfn "%A" (magnify v 2)             // 错误:无法传入Vector

  原因就是由于新增行magnify函数的参数变成了Point类型。为了传入 Vector类型,必须显式地表明参数是Vector类型的。改写成如下会怎么样呢?

  let magnify (v:Vector) n = { x = v.x * n; y = v.y * n}

  结果还是不行。再次改写下吧!

  let magnify (v:Vector) n : Vector = { x = v.x * n; y = v.y * n}

  这样就OK了。(v:Vector) 部分是指定参数的类型,而=之前的: Vector是用来指定函数的返回值的类型。

  总之,为了避免上述问题定义Record的时候最好不要让域值的名称相同。例如:

  type Vector = { dx: int; dy: int}

  这样,如果可以改变值域的名称,就不需要一一指定类型。不需要指定类型的实例代码如下:

type Vector = { dx: int; dy: int} 
type Point = { x: float; y: float} 
let    magnify v n = { dx = v.dx * n; dy = v.dy * n}
let v = { dx = 10; dy = 15 } 
printfn "%A" (magnify v 2)

  通常也是不需要指定类型的,不过最好也学习下显式指定类型的方法。这样对于比较大的程序就会有利于提高代码可读性。

 


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3