寓言实现外部接口编译错误
Fable implement foreign interface compile errors
TL;DR:
我正在尝试创建外部接口,但出现编译错误。我的 F# 技能还不能胜任这项任务。
我已经编写了一个 Photoshop 脚本,现在我正在尝试将该脚本转换为 Fable (F#) 以供我自己的 F# 练习使用。
旁注:我是 F# 的新手,但我有很好的 C# 背景。
要使用 photoshop 脚本库,我需要将其定义为外部接口 (http://fable.io/docs/interacting.html). I am trying to implement a small class first for testing purposes. Namely the UnitValue class (see https://wwwimages.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/JavaScriptToolsGuide_CS5.pdf p.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
class 看起来如何但不想在 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" 的 shorthand,它用于导入的 JS 结构,但您没有导入任何结构。参见 http://fable.io/docs/interacting.html
由于 JS 缺少类型,如果互操作是意图,您实际上必须自己在 F# 中定义类型。
如果互操作不是一个意图,那么写一个普通的 F# 代码,没有任何 Fable 属性,fable 会处理剩下的。
在此处为格式添加评论答案:
UnitValue 需要是一个函数(构造函数):
let UnitValue : unit->PSUnitValue = jsNative
那么你可以这样称呼它:
let test = UnitValue()
另请查看 this post 以了解有关绑定的一些背景知识。
在Eugene Tolmachev的帮助下:
工作(大概)代码:
#r "node_modules/fable-core/Fable.Core.dll"
open System
open Fable.Core
module Photoshop =
[<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 [<Global>] UnitValue : unit->PSUnitValue = jsNative
open Photoshop
let test = UnitValue();
test.``value`` <- 12.0;
test.``type`` <- PSUnit.Centimeters
Console.WriteLine (test.``as`` PSUnit.Millimeters)
输出
export var test = UnitValue(null);
test.value = 12;
test.type = "cm";
console.log(test.as("mm"));
我不知道 export
关键字在这里做什么,或者为什么 UnitValue 没有 new
关键字,但它编译并按照我的方向查找想要....
继续!
我现在在我的输出中有 new
关键字:
let [<Global;Emit("new [=12=](, )")>] UnitValue (v: float) (t: PSUnit): IUnitValue = jsNative
现在的用法是:
let test = UnitValue 12.0 PSUnit.Centimeters
Console.WriteLine (test.``as`` PSUnit.Millimeters)
TL;DR: 我正在尝试创建外部接口,但出现编译错误。我的 F# 技能还不能胜任这项任务。
我已经编写了一个 Photoshop 脚本,现在我正在尝试将该脚本转换为 Fable (F#) 以供我自己的 F# 练习使用。
旁注:我是 F# 的新手,但我有很好的 C# 背景。
要使用 photoshop 脚本库,我需要将其定义为外部接口 (http://fable.io/docs/interacting.html). I am trying to implement a small class first for testing purposes. Namely the UnitValue class (see https://wwwimages.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/JavaScriptToolsGuide_CS5.pdf p.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
class 看起来如何但不想在 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" 的 shorthand,它用于导入的 JS 结构,但您没有导入任何结构。参见 http://fable.io/docs/interacting.html由于 JS 缺少类型,如果互操作是意图,您实际上必须自己在 F# 中定义类型。
如果互操作不是一个意图,那么写一个普通的 F# 代码,没有任何 Fable 属性,fable 会处理剩下的。
在此处为格式添加评论答案: UnitValue 需要是一个函数(构造函数):
let UnitValue : unit->PSUnitValue = jsNative
那么你可以这样称呼它:
let test = UnitValue()
另请查看 this post 以了解有关绑定的一些背景知识。
在Eugene Tolmachev的帮助下:
工作(大概)代码:
#r "node_modules/fable-core/Fable.Core.dll"
open System
open Fable.Core
module Photoshop =
[<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 [<Global>] UnitValue : unit->PSUnitValue = jsNative
open Photoshop
let test = UnitValue();
test.``value`` <- 12.0;
test.``type`` <- PSUnit.Centimeters
Console.WriteLine (test.``as`` PSUnit.Millimeters)
输出
export var test = UnitValue(null);
test.value = 12;
test.type = "cm";
console.log(test.as("mm"));
我不知道 export
关键字在这里做什么,或者为什么 UnitValue 没有 new
关键字,但它编译并按照我的方向查找想要....
继续!
我现在在我的输出中有 new
关键字:
let [<Global;Emit("new [=12=](, )")>] UnitValue (v: float) (t: PSUnit): IUnitValue = jsNative
现在的用法是:
let test = UnitValue 12.0 PSUnit.Centimeters
Console.WriteLine (test.``as`` PSUnit.Millimeters)