TL; DR:
我试图创建一个外部接口,但是出现编译错误。我的F#技能还不等于任务。



我已经写了一个Photoshop脚本,现在我想根据自己的F#习惯将该脚本转换为寓言(F#)。

旁注:我是F#的新手,但是我有很好的C#背景。

要使用photoshop脚本库,我需要将其定义为外部接口(http://fable.io/docs/interacting.html)。我正在尝试为测试目的首先实现一个小类。即UnitValue类(请参见https://wwwimages.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/JavaScriptToolsGuide_CS5.pdf第230页)

我的尝试:

#r "node_modules/fable-core/Fable.Core.dll"

open System
open Fable.Core

[<Global>]
module Photoshop =
    [<Erase;StringEnum>]
    type PSUnit =
        | [<CompiledName("in")>] Inches
        | [<CompiledName("ft")>] Feet
        | [<CompiledName("yd")>] Yards
        | [<CompiledName("mi")>] Miles
        | [<CompiledName("mm")>] Millimeters
        | [<CompiledName("cm")>] Centimeters
        | [<CompiledName("m")>] Meters
        | [<CompiledName("km")>] Kilometers
        | [<CompiledName("pt")>] Points
        | [<CompiledName("pc")>] Picas
        | [<CompiledName("tpt")>] TraditionalPoints
        | [<CompiledName("tpc")>] TraditionalPicas
        | [<CompiledName("ci")>] Ciceros
        | [<CompiledName("px")>] Pixels
        | [<CompiledName("%")>] Percent

    type PSUnitValue =
        abstract ``as``: PSUnit -> float
        abstract ``value``: float with get, set
        abstract ``type``: PSUnit with get, set

    let UnitValue : PSUnitValue = jsNative

open Photoshop

let test = new UnitValue();
test.``value`` = 12.0;
test.``type`` = Unit.Centimeters

Console.WriteLine (test.``as`` PSUnit.Millimeters)


在编译过程中,我得到:

C:\PsScripts>fable test.fsx
fable-compiler 0.7.28: Start compilation...
F# project contains errors:
C:\PsScripts\test.fsx(L35,15) : error FSHARP: The type 'UnitValue' is not defined
C:\PsScripts\test.fsx(L36,0) : error FSHARP: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.
C:\PsScripts\test.fsx(L37,0) : error FSHARP: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.
C:\PsScripts\test.fsx(L37,21) : error FSHARP: The field, constructor or member 'Centimeters' is not defined
C:\PsScripts\test.fsx(L39,19) : error FSHARP: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.
C:\PsScripts\test.fsx(L39,0) : error FSHARP: A unique overload for method 'WriteLine' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: Console.WriteLine(buffer: char []) : unit, Console.WriteLine(format: string, [<ParamArray>] arg: obj []) : unit, Console.WriteLine(value: bool) : unit, Console.WriteLine(value: char) : unit, Console.WriteLine(value: decimal) : unit, Console.WriteLine(value: float) : unit, Console.WriteLine(value: float32) : unit, Console.WriteLine(value: int) : unit, Console.WriteLine(value: int64) : unit, Console.WriteLine(value: obj) : unit, Console.WriteLine(value: string) : unit, Console.WriteLine(value: uint32) : unit, Console.WriteLine(value: uint64) : unit


我的预期输出将是这样的:

var test = new UnitValue();
test.value = 12;
test.unit = "cm";
console.log(test.as("mm"));




对于那些想知道UnitValue类的外观并且不想在pdf中查找的人,在这里我用C#定义了它:

public class UnitValue
{
    // Static baseUnit not yet implemented in Fable
    public static UnitValue baseUnit { get; set; }

    // Constructors not yet implemented in Fable
    public UnitValue(float value, string unit) { }
    public UnitValue(string valueUnit) { }

    public string type { get; set; }
    public float value { get; set; }

    public float @as(string unit) { throw new NotImplementedException(); }

    // I don't use convert in my scripts, so not yet implemented in Fable as well
    public UnitValue convert(string unit) { throw new NotImplementedException(); }
}

最佳答案

您正在绑定变量UnitValue,而不是为类型声明别名(不确定是否是意图)。
就像C#中一样,类型和值具有不同的作用域,因此就编译器而言,该类型和值未声明。
jsNative只是“ throw”的简写,它用于导入的JS构造,但是您不导入任何内容。见http://fable.io/docs/interacting.html
由于JS缺少类型,因此如果要使用互操作,则实际上必须自己在F#中定义类型。
如果互操作不是意图,则编写没有任何寓言属性的普通F#代码,寓言将处理其余工作。


在此处添加注释答案以进行格式设置:
UnitValue必须是一个函数(构造函数):

let UnitValue : unit->PSUnitValue = jsNative


那么你可以称之为:

let test = UnitValue()


还要检查this post绑定的背景知识。

关于f# - 寓言实现外部接口(interface)编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41615341/

10-11 11:14