在F#,Nominative还是Structural?答案说,我可以通过一些奇特的机制欺骗F#像结构类型的语言一样工作.我怎么能这样做?
例如,如果我尝试扩展int,而int不是该类型的真实名称,则此代码将失败:
type int with
member this.IsEven = this % 2 = 0
Run Code Online (Sandbox Code Playgroud)
我必须System.Int32改用:
type System.Int32 with
member this.IsEven = this % 2 = 0
//test
let i = 20
if i.IsEven then printfn "'%i' is even" i
Run Code Online (Sandbox Code Playgroud)
为什么我不能使用类型缩写int?
在函数式编程中,调用不接受参数的函数的正确名称是什么?例如:
// 2 types of execute functions
type Function =
| UnitFunction of (unit -> unit)
| OperandFunction of (unit16 -> unit)
let nop () =
()
let shiftRegisterA operand =
registers.A <- registers.A <<< operand
Run Code Online (Sandbox Code Playgroud)
可以nop称为a UnitFunction,shiftRegisterA被称为OperandFunction?
如何->在模式匹配后获得推断类型?
例如:
type Type =
| Complex1
| Complex2
| Number of int
| Integer of int
let calculate = function
| Number i -> Number (i + 1)
| Integer i -> Integer (i + 1)
| Complex1 | Complex2 as t -> t
Run Code Online (Sandbox Code Playgroud)
我想通过合并来缩短这个功能Number并Integer用或|图案.如何获取推断的案例标识符以使以下功能起作用:
let calculate' = function
| Number i | Integer i ->
// If this is Number, return Number (i + 1)
// Else return Integer (i …Run Code Online (Sandbox Code Playgroud) 根据这个被接受的答案,在F#和OCaml中,我需要使用下划线来丢弃记录的其余部分.但是,为什么handle'功能有效但handle功能不起作用?
type Type = Type of int
type Entity =
{ type' : Type
foo : string }
let handle entities =
match entities with
| {type' = Type i; _ }::entites -> ()
| [] -> ()
let handle' entities =
match entities with
| {type' = Type i }::entites -> ()
| [] -> ()
Run Code Online (Sandbox Code Playgroud) 基于“F#中关键字“in”的含义”的回答:
let (x = 2 and y = x + 2) in
y + x
Run Code Online (Sandbox Code Playgroud)
这不会像
let (x = 2 and y = x + 2)
y + x
Run Code Online (Sandbox Code Playgroud)
在前一种情况下x,只绑定在in关键字之后。在后一种情况下,正常的变量作用域规则生效,因此变量一经声明就被绑定。
当需要使用 , 指定绑定变量in而不是声明时绑定?
F#记录不能被继承,但是它们可以实现接口。例如,我想创建不同的控制器:
type ControllerType =
| Basic
| Advanced1
| Advanced1RAM
| Advanced1RAMBattery
| Advanced2
// base abstract class
type IController =
abstract member rom : byte[]
abstract member ``type`` : ControllerType
type BasicController =
{ rom : byte[]
``type`` : ControllerType }
interface IController with
member this.rom = this.rom
member this.``type`` = this.``type``
type AdvancedController1 =
{ ram : byte[]
rom : byte[]
``type`` : ControllerType }
interface IController with
member this.rom = this.rom
member this.``type`` = this.``type``
type AdvancedController2 …Run Code Online (Sandbox Code Playgroud) 问题:
继承依赖
类型A在属性中存储类型B的值
类型B继承自类型A.
我们将考虑一个UI控件层次结构,其中每个控件都属于顶级"Form",而Form本身就是一个Control.
作者通过参数化类解决了这个问题:
[<AbstractClass>]
type Control<'Form>(name) =
member this.Name = name
abstract Form : 'Form
type Form(name) =
inherit Control<Form>(name)
override this.Form = this
type Button(name,form) =
inherit Control<Form>(name)
override this.Form = form
let form = new Form("form")
let button = new Button("button",form)
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能在功能方法中重新设计:"我们根本不会使用继承.相反,我们会将组合与参数化结合使用"?
我写了一个带有2个列表的点积函数:
let inline dot a b =
List.zip a b
|> List.map (fun (a, b) -> a * b)
|> List.reduce (+)
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来计算点积而不使用List.zip?
我有一个非空终止的字符向量,如何构造一个新字符串并让它自动插入\0到字符串的末尾?
std::vector<char> v;
v.push_back('H');
v.push_back('i');
v.push_back('!');
//v.push_back('\0'); <~ without using this line
std::string a(v.data());
std::string b(v.begin(), v.end()); // same meaning as b(v.data(), v.size())
Run Code Online (Sandbox Code Playgroud)
两者之间的正确构造函数是什么?
来自Array Covariance文章:
元素类型是引用类型的数组是协变的.[...]它被添加到CLR中,因为Java需要它,而CLR设计者希望能够支持类似Java的语言.
CLR支持数组协方差,但是如何在F#中使用这个方便的功能?
type Foo () = class end
type Bar () = inherit Foo ()
// must have type Foo []
let foo : Foo [] = Array.create 1 (Bar ())
// ^^^^^^ must not cast to Foo
// must throw ArrayTypeMismatchException
foo.[0] <- Foo ()
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我想在场景后面foo存储一个Bar数组,就像在CLR中实现一样.
物体可以向任一方向铸造,这是不安全的.如何强制任何对象为null并在生产代码中使用它们?
例如,在Option<'T>,我现在可以轻松地以3种方式,使可空类型:Some (Unchecked.defaultof<'T>),None和null.
根据正式的Pascal EBNF定义(pg69-75),我看到Pascal只支持3种原始类型:Integer,Real和String.
在C中,任何不同的值都0可以解释为true文字.Pascal不像C一样工作.当Pascal没有布尔类型时,它如何处理条件表达式?