gpt4 book ai didi

static - F# : static let and static member

转载 作者:行者123 更新时间:2023-12-03 20:34:21 25 4
gpt4 key购买 nike

我是 F# 的菜鸟,目前正在阅读 F# 3.0 中的专家。
它是我学习的第一种编译语言(我只知道用 R 编程)

在第 6 章第 117 页,我们没有太多仪式性地介绍
静态让和静态成员。我真的不明白它是什么

type Vector2D(dx : float, dy : float) =
static let zero = Vector2D(0.0, 0.0)
static let onex = Vector2D(1.0, 0.0)
static let oney = Vector2D(0.0, 1.0)
/// Get the zero vector
static member Zero = zero
/// Get a constant vector along the X axis of length one
static member OneX = onex
/// Get a constant vector along the Y axis of length one
static member OneY = oney

书中没有进一步说明谁的例子。

我在 F# 交互中输入这个。从那里,我如何构造一个值为零(或 onex ...)的变量 x ?

我正在尝试以下操作。没有任何效果
let x = Zero;;
let y = Vector2D(2.0,2.0);; /// ok
y.Zero;;
stdin(237,1): error FS0809: Property 'Zero' is static

http://fsharpforfunandprofit.com/posts/classes/
有这个例子,
type StaticExample() = 
member this.InstanceValue = 1
static member StaticValue = 2 // no "this"

// test
let instance = new StaticExample()
printf "%i" instance.InstanceValue
printf "%i" StaticExample.StaticValue

所以我会期待 y.Zero;;产生以上的东西?...

谢谢。对不起,这个问题太基本了。如果有人可以解释我这是关于什么的...

最佳答案

所以基本的区别是静态成员不属于类的实例。

而不是 y.Zero , 你得到 Vector2D.Zero .

对于第一个近似值,您可以考虑这些类型属性的示例。

关于static - F# : static let and static member,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34447766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com