|
| 1 | +module Fable.Core.PyInterop |
| 2 | + |
| 3 | +open System |
| 4 | +open Fable.Core |
| 5 | + |
| 6 | +/// Has same effect as `unbox` (dynamic casting erased in compiled Python code). |
| 7 | +/// The casted type can be defined on the call site: `!!myObj?bar(5): float` |
| 8 | +let (!!) x: 'T = pyNative |
| 9 | + |
| 10 | +/// Implicit cast for erased unions (U2, U3...) |
| 11 | +let inline (!^) (x:^t1) : ^t2 = ((^t1 or ^t2) : (static member op_ErasedCast : ^t1 -> ^t2) x) |
| 12 | + |
| 13 | +/// Dynamically access a property of an arbitrary object. |
| 14 | +/// `myObj?propA` in Python becomes `myObj.propA` |
| 15 | +/// `myObj?(propA)` in Python becomes `myObj[propA]` |
| 16 | +let (?) (o: obj) (prop: obj): 'a = pyNative |
| 17 | + |
| 18 | +/// Dynamically assign a value to a property of an arbitrary object. |
| 19 | +/// `myObj?propA <- 5` in Python becomes `myObj.propA = 5` |
| 20 | +/// `myObj?(propA) <- 5` in Python becomes `myObj[propA] = 5` |
| 21 | +let (?<-) (o: obj) (prop: obj) (v: obj): unit = pyNative |
| 22 | + |
| 23 | +/// Destructure and apply a tuple to an arbitrary value. |
| 24 | +/// E.g. `myFn $ (arg1, arg2)` in Python becomes `myFn(arg1, arg2)` |
| 25 | +let ($) (callee: obj) (args: obj): 'a = pyNative |
| 26 | + |
| 27 | +/// Upcast the right operand to obj (and uncurry it if it's a function) and create a key-value tuple. |
| 28 | +/// Mostly convenient when used with `createObj`. |
| 29 | +/// E.g. `createObj [ "a" ==> 5 ]` in Python becomes `{ a: 5 }` |
| 30 | +let (==>) (key: string) (v: obj): string*obj = pyNative |
| 31 | + |
| 32 | +/// Destructure a tuple of arguments and applies to literal Python code as with EmitAttribute. |
| 33 | +/// E.g. `emitPyExpr (arg1, arg2) "$0 + $1"` in Python becomes `arg1 + arg2` |
| 34 | +let emitPyExpr<'T> (args: obj) (pyCode: string): 'T = pyNative |
| 35 | + |
| 36 | +/// Same as emitPyExpr but intended for Python code that must appear in a statement position |
| 37 | +/// E.g. `emitPyExpr aValue "while($0 < 5) doSomething()"` |
| 38 | +let emitPyStatement<'T> (args: obj) (pyCode: string): 'T = pyNative |
| 39 | + |
| 40 | +/// Create a literal Python object from a collection of key-value tuples. |
| 41 | +/// E.g. `createObj [ "a" ==> 5 ]` in Python becomes `{ a: 5 }` |
| 42 | +let createObj (fields: #seq<string*obj>): obj = pyNative |
| 43 | + |
| 44 | +/// Create a literal Python object from a collection of union constructors. |
| 45 | +/// E.g. `keyValueList CaseRules.LowerFirst [ MyUnion 4 ]` in Python becomes `{ myUnion: 4 }` |
| 46 | +let keyValueList (caseRule: CaseRules) (li: 'T seq): obj = pyNative |
| 47 | + |
| 48 | +/// Create a literal Py object from a mutator lambda. Normally used when |
| 49 | +/// the options interface has too many fields to be represented with a Pojo record. |
| 50 | +/// E.g. `pyOptions<MyOpt> (fun o -> o.foo <- 5)` in Python becomes `{ "foo": 5 }` |
| 51 | +let pyOptions<'T> (f: 'T->unit): 'T = pyNative |
| 52 | + |
| 53 | +/// Create an empty Python object: {} |
| 54 | +let createEmpty<'T> : 'T = pyNative |
| 55 | + |
| 56 | +[<Emit("type($0)")>] |
| 57 | +let pyTypeof (x: obj): string = pyNative |
| 58 | + |
| 59 | +[<Emit("isinstance($0, $1)")>] |
| 60 | +let pyInstanceof (x: obj) (cons: obj): bool = pyNative |
| 61 | + |
| 62 | +/// Works like `ImportAttribute` (same semantics as ES6 imports). |
| 63 | +/// You can use "*" or "default" selectors. |
| 64 | +let import<'T> (selector: string) (path: string):'T = pyNative |
| 65 | + |
| 66 | +/// F#: let myMember = importMember<string> "myModule" |
| 67 | +/// Py: from my_module import my_member |
| 68 | +/// Note the import must be immediately assigned to a value in a let binding |
| 69 | +let importMember<'T> (path: string):'T = pyNative |
| 70 | + |
| 71 | +/// F#: let myLib = importAll<obj> "myLib" |
| 72 | +/// Py: from my_lib import * |
| 73 | +let importAll<'T> (path: string):'T = pyNative |
| 74 | + |
| 75 | +/// Imports a file only for its side effects |
| 76 | +let importSideEffects (path: string): unit = pyNative |
0 commit comments