相关疑难解决方法(0)

Static extension methods supporting member constraints

I need to implement a static extension method supporting member constraints on some basic primitive types like integers, floats, etc. Here's my code for signed integers:

module MyOperators =
    let inline foo (x : ^T) = (^T : (static member Foo : ^T -> int) (x)) 

    type System.Int32 with 
        static member Foo(x : Int32) = 7 // static extension
Run Code Online (Sandbox Code Playgroud)

Test code:

open MyOperators    
let x = foo 5 // x should be 7
Run Code Online (Sandbox Code Playgroud)

But compiler complains with error:

The type …

extension-methods f# type-constraints

5
推荐指数
1
解决办法
656
查看次数

通用指示符'和符号^在F#方法签名之间有什么区别

我理解用于表示通用参数的刻度,如:

Seq.append : seq<'T> -> seq<'T> -> seq<'T>
Run Code Online (Sandbox Code Playgroud)

但是插入符号表示什么,如:

Seq.average : seq<^T> -> ^T
Run Code Online (Sandbox Code Playgroud)

generics f# method-signature

5
推荐指数
2
解决办法
210
查看次数

静态分辨的类型参数

以下(简化)代码段取自我正在实现的应用程序,该应用程序始终使用静态解析的类型参数.

type A< ^B when ^B : (static member MyMember : Unit -> Unit)> = {
  Field : unit
}
type TestA = {
  AField : A< BTy >
}
and BTy = {
  BField : Unit
} with
  static member MyMember () = ()
Run Code Online (Sandbox Code Playgroud)

当我定义字段AField(AField : A< BTy >)的类型时,IntelliSense给出了以下错误:类型'BTy'不支持任何名为'MyMember'的运算符.

编辑.单独声明它们是有效的,但如果我有一个共同引用,我不能声明第三种类型放在顶部,其中包含两种类型的公共信息.我该怎么做才能避免这个问题?无论如何,如果我在下面定义let pluto = ("" :> obj) :?> A< BTy >它的定义,我想是因为这两种类型都是从let绑定中可见的.

parameters f# types

5
推荐指数
2
解决办法
1365
查看次数

F#:成员约束有助于创建看似动态的类型

我一直在寻找一种方法来为F#方法添加一些鸭子类型.

SomeMethod(model:'a) =
   let someField = model.Test("")
Run Code Online (Sandbox Code Playgroud)

进来的参数有Test方法.我看过这样的符号:

member inline public x.Testing< ^a when ^a : (member public Test : String-> String)>(model:^a) =   
  let something = model.Test("")
  ignore
Run Code Online (Sandbox Code Playgroud)

对我来说,通用约束可以用于方法级别而不是类/接口级别.问题是由于类型问题我无法编译它.这让我相信没有办法在方法级别指定约束.这是coorect?

generics f# generic-constraints

3
推荐指数
1
解决办法
190
查看次数

无法扩展F#中的运算符?

module FSharp=
let Point2d (x,y)= Point2d(x,y)
let Point3d (x,y,z)= Point3d(x,y,z)
type NXOpen.Point3d with
    static member ( * ) (p:Point3d,t:float)= Point3d(p.X*t,p.Y*t,p.Z*t)
    static member ( * ) (t:float,p:Point3d)= Point3d(p.X*t,p.Y*t,p.Z*t)
    static member (+) (p:Point3d,t:float)= Point3d(p.X+t,p.Y+t,p.Z+t)
    static member (+) (t:float,p:Point3d)= Point3d(p.X+t,p.Y+t,p.Z+t)
    static member (+) (p:Point3d,t:Point3d)= Point3d(p.X+t.X,p.Y+t.Y,p.Z+t.Z)

let a=Point3d (1.,2.,3.)
let b=1.0
let c=a * b//error
Run Code Online (Sandbox Code Playgroud)

错误15:类型'float'与
'Point3d' 类型不匹配E:\ Work\extension-RW\VS\extension\NXOpen.Extension.FSharp\Module1.fs 18 13 NXOpen.Extension.FSharp

我想扩展Point3d方法,一些新的运算符.但它并没有过去.

extension-methods f# operators

3
推荐指数
1
解决办法
427
查看次数

为什么我们将不带正数的字面量称为目标子句呢?

我想我知道目标从句号角从句的含义,但是我对人们为什么命名一个不带正向目标的字面量的歧义感到困惑。

这里的目标是什么?

logic prolog

3
推荐指数
2
解决办法
414
查看次数

F#:转换为泛型类型

我是F#的新手,来自C++背景.我正在尝试编写一个简单的向量类,它可以是泛型类型(int,float等),但我遇到了默认构造函数的问题.我想将值初始化为零,但为了做到这一点,我需要以某种方式将一个具体的零转换为泛型类型,但我不知道如何做到这一点.

也许一些代码可能有所帮助 这是我到目前为止所拥有的:

type Vector3D<'T> (x :'T, y: 'T, z: 'T) = 
    member this.x = x
    member this.y = y
    member this.z = z

    new() = Vector3D<'T>(0,0,0) // what goes here?
Run Code Online (Sandbox Code Playgroud)

我在突出显示的行上尝试了很多东西,但似乎无法让编译器感到高兴.例如,我试过,Vector3D('T 0, 'T 0, 'T 0)我认为应该将int'T归零,但这不起作用.

我错过了一些基本的东西,还是仅仅是获得正确语法的情况?

generics f# casting

2
推荐指数
1
解决办法
813
查看次数

F#缺少类型约束

在以下代码中,请注意get_Zero的类型约束:

type Wrapper<'t> = { Data : 't[] }

let compute<'t
    when 't : (static member get_Zero : unit -> 't)
    and 't : (static member (~-) : 't -> 't)
    and 't : (static member (+) : 't * 't -> 't)>
        (wrapper : Wrapper<'t>) =
    wrapper.Data
        |> Seq.mapi (fun i value -> (i, value))
        |> Seq.sumBy (fun (i, value) ->
            if i % 2 = 0 then value
            else -value)
Run Code Online (Sandbox Code Playgroud)

即使我已经有一个显式类型约束,我仍然在调用Seq.sumBy时遇到以下编译器错误:

类型参数缺少约束'when ^ t :(静态成员get_Zero: - > ^ …

f# type-constraints

2
推荐指数
1
解决办法
274
查看次数

类型(单位 - >单位)的函数是否可以在F#中静态解析类型参数?

为什么不允许这样做?

type Foo() =
    static member Bar() = ()

let inline bar<^a>() = //ERROR: unexpected infix operator in pattern
    (^a : (static member Bar : unit -> unit)())

//Hypothetical usage: let _ = bar<Foo>()
Run Code Online (Sandbox Code Playgroud)

......但是这样可以吗?

type Foo() =
    static member Bar() = new Foo()

let inline bar() : ^a =
    (^a : (static member Bar : unit -> ^a)())

let x : Foo = bar()
Run Code Online (Sandbox Code Playgroud)

具有静态解析类型参数的函数是否需要返回已解析类型的实例?

f# inline

1
推荐指数
1
解决办法
383
查看次数

F#Generic Math:如何使用op_GreaterThan编写函数

在F#中,如何编写泛型数学步骤函数?

(Oliver)Heaviside阶跃函数是函数,如果x为负,则返回零,否则返回一个.

以下是我到目前为止的尝试摘要:

// attempt 1:
let inline stepFct1< ^T when ^T : (static member op_GreaterThan: ^T * float -> bool) 
    >     (x:^T) : ^T = 
    //if (^T : (static member op_GreaterThan) (x 0.0) ) then x  //ouch fails also
    if  (>) x 0.0 then x
    else 0.0
Run Code Online (Sandbox Code Playgroud)

编译器说:错误FS0001:类型参数缺少约束'当^ T:比较'

// attempt 2:
let inline stepFct2<^T when ^T : (static member (>): ^T * ^T -> bool) > (x:^T) : ^T = 
    match x with 
    | x when x > …
Run Code Online (Sandbox Code Playgroud)

generics math f# diffsharp

0
推荐指数
1
解决办法
182
查看次数

F#神秘约束错误消息

为了好玩,我一直在玩F#中的类型类,使用这里显示的想法.我创建了一个Next类型类来表示具有"后继"的值,例如下一个1 = 2,下一个今天=明天等:

type Next = Next with
    static member (++) (Next, x:int) = x + 1
    static member (++) (Next, x:DateTime) = x.AddDays 1.0
let inline next x = Next ++ x 

let v1 = next 1 // v1 = 2
let v2 = next DateTime.Now // v2 = Now + 1 day
Run Code Online (Sandbox Code Playgroud)

现在我想在泛型类中使用"nextable"类型:

type UsesNextable<'T>(nextable: 'T) = // Compile error 1
    member inline this.Next = 
        let v = next nextable         // Compile error 2
        v.ToString()
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误: …

f#

0
推荐指数
1
解决办法
221
查看次数