Skip to content

Commit 0cf8b3d

Browse files
committed
doc: updates
1 parent c9faf18 commit 0cf8b3d

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

docs-src/communicate/py-from-fable.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Here we use the `ImportAll` attribute, to import the module.
164164

165165
- step 1: We specify the module we wish to use. Here `alert` means: "import the `alert.py` file".
166166
- step 2: we create a let binding called `mylib` to map the js library.
167-
- step 3: we use the `nativeOnly` keyword to say that `mylib` is just a placeholder for the JavaScript native implementation.
167+
- step 3: we use the `nativeOnly` keyword to say that `mylib` is just a placeholder for the Python native implementation.
168168

169169
Now we can use this:
170170

@@ -486,14 +486,14 @@ type MyStrings =
486486
myLib.myMethod(Vertical, Horizontal)
487487
```
488488

489-
```js
490-
// js output
491-
myLib.myMethod("vertical", "Horizontal");
489+
```py
490+
// Python output
491+
myLib.myMethod("vertical", "Horizontal")
492492
```
493493

494494
## Plain Old JavaScript Objects
495495

496-
To create a plain JS object (aka POJO), use `createObj`:
496+
To create plain Python dictionaries, use `createObj`:
497497

498498
```fsharp
499499
open Fable.Core.PyInterop
@@ -516,7 +516,7 @@ let data =
516516
```
517517

518518
:::{note}
519-
Since fable-compiler 2.3.6, when using the dynamic cast operator `!!` to cast an anonymous record to an interface, Fable will raise a warning if the fields in the anonymous don't match those of the interface. Use this feature only to interop with JS, in F# code the proper way to instantiate an interface without an implementing type is an [object expression](https://fsharpforfunandprofit.com/posts/object-expressions/).
519+
Since fable-compiler 2.3.6, when using the dynamic cast operator `!!` to cast an anonymous record to an interface, Fable will raise a warning if the fields in the anonymous don't match those of the interface. Use this feature only to interop with Python, in F# code the proper way to instantiate an interface without an implementing type is an [object expression](https://fsharpforfunandprofit.com/posts/object-expressions/).
520520
:::
521521

522522
```fsharp
@@ -535,7 +535,7 @@ let y: IMyInterface = !!{| foo = "5"; bAr = 4.; baz = Some 0 |}
535535
let z: IMyInterface = !!{| foo = "5"; bar = 4. |}
536536
```
537537

538-
You can also create a JS object from an interface by using `createEmpty` and then assigning the fields manually:
538+
You can also create a Python dictinary from an interface by using `createEmpty` and then assigning the fields manually:
539539

540540
```fsharp
541541
let x = createEmpty<IMyInterface> // var x = {}
@@ -546,7 +546,7 @@ x.bar <- 8.5 // val.bar = 8.5
546546
A similar solution that can also be optimized by Fable directly into a JS object at compile time is to use the `jsOptions` helper:
547547

548548
```fsharp
549-
let x = jsOptions<IMyInterface>(fun x ->
549+
let x = pyOptions<IMyInterface>(fun x ->
550550
x.foo <- "abc"
551551
x.bar <- 8.5)
552552
```
@@ -579,19 +579,25 @@ it's usually a good idea to inline the function containing the helper.
579579

580580
### Dynamic typing: don't read this!
581581

582-
Through the use of the tools we just described above, Fable guarantees you shouldn't run into nasty bugs (as long as the interface contracts are correct) because all the code will be checked by the compiler. If it does not compile it either means your Python library does not exist or its path is not good or that your F# implementation lacks something. We do rely on Fable on systems that are used 24/7. We know that if it compiles, it means a 99% chance of running without any problems.
582+
Through the use of the tools we just described above, Fable guarantees you shouldn't run into nasty bugs (as long as the
583+
interface contracts are correct) because all the code will be checked by the compiler. If it does not compile it either
584+
means your Python library does not exist or its path is not good or that your F# implementation lacks something. We do
585+
rely on Fable on systems that are used 24/7. We know that if it compiles, it means a 99% chance of running without any
586+
problems.
583587

584588
Our motto is: "If it compiles, it works!"
585589

586-
Still, like we stated, **interop is a question of trust**. If you trust your Python code and F# code, then maybe it's ok to do things together without further checks. Maybe.
590+
Still, like we stated, **interop is a question of trust**. If you trust your Python code and F# code, then maybe it's ok
591+
to do things together without further checks. Maybe.
587592

588593
:::{warning}
589594
Disclaimer: use this at your own risk
590595
:::
591596

592597
#### What is dynamic typing?
593598

594-
Fable.Core.PyInterop implements the F# dynamic operators so you can easily access an object property by name (without static check) as follows:
599+
Fable.Core.PyInterop implements the F# dynamic operators so you can easily access an object property by name (without
600+
static check) as follows:
595601

596602
```fsharp
597603
open Fable.Core.PyInterop
@@ -607,7 +613,8 @@ printfn "Value: %O" jsObject?(pname) // Access with a reference
607613
pyObject?myProperty <- 5 // Assignment is also possible
608614
```
609615

610-
When you combine the dynamic operator with application, Fable will destructure tuple arguments as with normal method calls. These operations can also be chained to replicate Python fluent APIs.
616+
When you combine the dynamic operator with application, Fable will destructure tuple arguments as with normal method
617+
calls. These operations can also be chained to replicate Python fluent APIs.
611618

612619
```fsharp
613620
let result = pyObject?myMethod(1, 2)

docs/communicate/py-from-fable.html

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ <h3>Let’s practice! 1st try!<a class="headerlink" href="#let-s-practice-1st-tr
714714
<ul class="simple">
715715
<li><p>step 1: We specify the module we wish to use. Here <code class="docutils literal notranslate"><span class="pre">alert</span></code> means: “import the <code class="docutils literal notranslate"><span class="pre">alert.py</span></code> file”.</p></li>
716716
<li><p>step 2: we create a let binding called <code class="docutils literal notranslate"><span class="pre">mylib</span></code> to map the js library.</p></li>
717-
<li><p>step 3: we use the <code class="docutils literal notranslate"><span class="pre">nativeOnly</span></code> keyword to say that <code class="docutils literal notranslate"><span class="pre">mylib</span></code> is just a placeholder for the JavaScript native implementation.</p></li>
717+
<li><p>step 3: we use the <code class="docutils literal notranslate"><span class="pre">nativeOnly</span></code> keyword to say that <code class="docutils literal notranslate"><span class="pre">mylib</span></code> is just a placeholder for the Python native implementation.</p></li>
718718
</ul>
719719
<p>Now we can use this:</p>
720720
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="n">mylib</span><span class="o">.</span><span class="n">triggerAlert</span><span class="w"> </span><span class="o">(</span><span class="s">&quot;Hey I&#39;m calling my js library from Fable &gt; &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">mylib</span><span class="o">.</span><span class="n">someString</span><span class="o">)</span><span class="w"></span>
@@ -996,15 +996,15 @@ <h3>StringEnum<a class="headerlink" href="#stringenum" title="Permalink to this
996996
<span class="n">myLib</span><span class="o">.</span><span class="n">myMethod</span><span class="o">(</span><span class="n">Vertical</span><span class="o">,</span><span class="w"> </span><span class="n">Horizontal</span><span class="o">)</span><span class="w"></span>
997997
</pre></div>
998998
</div>
999-
<div class="highlight-js notranslate"><div class="highlight"><pre><span></span><span class="c1">// js output</span><span class="w"></span>
1000-
<span class="nx">myLib</span><span class="p">.</span><span class="nx">myMethod</span><span class="p">(</span><span class="s2">&quot;vertical&quot;</span><span class="p">,</span><span class="w"> </span><span class="s2">&quot;Horizontal&quot;</span><span class="p">);</span><span class="w"></span>
999+
<div class="highlight-py notranslate"><div class="highlight"><pre><span></span><span class="o">//</span> <span class="n">Python</span> <span class="n">output</span>
1000+
<span class="n">myLib</span><span class="o">.</span><span class="n">myMethod</span><span class="p">(</span><span class="s2">&quot;vertical&quot;</span><span class="p">,</span> <span class="s2">&quot;Horizontal&quot;</span><span class="p">)</span>
10011001
</pre></div>
10021002
</div>
10031003
</section>
10041004
</section>
10051005
<section id="plain-old-javascript-objects">
10061006
<h2>Plain Old JavaScript Objects<a class="headerlink" href="#plain-old-javascript-objects" title="Permalink to this headline">#</a></h2>
1007-
<p>To create a plain JS object (aka POJO), use <code class="docutils literal notranslate"><span class="pre">createObj</span></code>:</p>
1007+
<p>To create plain Python dictionaries, use <code class="docutils literal notranslate"><span class="pre">createObj</span></code>:</p>
10081008
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="k">open</span><span class="w"> </span><span class="nn">Fable.Core.PyInterop</span><span class="w"></span>
10091009

10101010
<span class="k">let</span><span class="w"> </span><span class="nv">data</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
@@ -1024,7 +1024,7 @@ <h2>Plain Old JavaScript Objects<a class="headerlink" href="#plain-old-javascrip
10241024
</div>
10251025
<div class="admonition note">
10261026
<p class="admonition-title">Note</p>
1027-
<p>Since fable-compiler 2.3.6, when using the dynamic cast operator <code class="docutils literal notranslate"><span class="pre">!!</span></code> to cast an anonymous record to an interface, Fable will raise a warning if the fields in the anonymous don’t match those of the interface. Use this feature only to interop with JS, in F# code the proper way to instantiate an interface without an implementing type is an <a class="reference external" href="https://fsharpforfunandprofit.com/posts/object-expressions/">object expression</a>.</p>
1027+
<p>Since fable-compiler 2.3.6, when using the dynamic cast operator <code class="docutils literal notranslate"><span class="pre">!!</span></code> to cast an anonymous record to an interface, Fable will raise a warning if the fields in the anonymous don’t match those of the interface. Use this feature only to interop with Python, in F# code the proper way to instantiate an interface without an implementing type is an <a class="reference external" href="https://fsharpforfunandprofit.com/posts/object-expressions/">object expression</a>.</p>
10281028
</div>
10291029
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="k">type</span><span class="w"> </span><span class="nc">IMyInterface</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
10301030
<span class="w"> </span><span class="k">abstract</span><span class="w"> </span><span class="n">foo</span><span class="o">:</span><span class="w"> </span><span class="kt">string</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="n">get</span><span class="o">,</span><span class="w"> </span><span class="n">set</span><span class="w"></span>
@@ -1041,14 +1041,14 @@ <h2>Plain Old JavaScript Objects<a class="headerlink" href="#plain-old-javascrip
10411041
<span class="k">let</span><span class="w"> </span><span class="nv">z</span><span class="o">:</span><span class="w"> </span><span class="n">IMyInterface</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">!!{|</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;5&quot;</span><span class="o">;</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="o">.</span><span class="w"> </span><span class="o">|}</span><span class="w"></span>
10421042
</pre></div>
10431043
</div>
1044-
<p>You can also create a JS object from an interface by using <code class="docutils literal notranslate"><span class="pre">createEmpty</span></code> and then assigning the fields manually:</p>
1044+
<p>You can also create a Python dictinary from an interface by using <code class="docutils literal notranslate"><span class="pre">createEmpty</span></code> and then assigning the fields manually:</p>
10451045
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="k">let</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">createEmpty</span><span class="o">&lt;</span><span class="n">IMyInterface</span><span class="o">&gt;</span><span class="w"> </span><span class="c1">// var x = {}</span><span class="w"></span>
10461046
<span class="n">x</span><span class="o">.</span><span class="n">foo</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="s">&quot;abc&quot;</span><span class="w"> </span><span class="c1">// x.foo = &quot;abc&quot;</span><span class="w"></span>
10471047
<span class="n">x</span><span class="o">.</span><span class="n">bar</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="mi">8</span><span class="o">.</span><span class="mi">5</span><span class="w"> </span><span class="c1">// val.bar = 8.5</span><span class="w"></span>
10481048
</pre></div>
10491049
</div>
10501050
<p>A similar solution that can also be optimized by Fable directly into a JS object at compile time is to use the <code class="docutils literal notranslate"><span class="pre">jsOptions</span></code> helper:</p>
1051-
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="k">let</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">jsOptions</span><span class="o">&lt;</span><span class="n">IMyInterface</span><span class="o">&gt;(</span><span class="k">fun</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"></span>
1051+
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="k">let</span><span class="w"> </span><span class="nv">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pyOptions</span><span class="o">&lt;</span><span class="n">IMyInterface</span><span class="o">&gt;(</span><span class="k">fun</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"></span>
10521052
<span class="w"> </span><span class="n">x</span><span class="o">.</span><span class="n">foo</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="s">&quot;abc&quot;</span><span class="w"></span>
10531053
<span class="w"> </span><span class="n">x</span><span class="o">.</span><span class="n">bar</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="mi">8</span><span class="o">.</span><span class="mi">5</span><span class="o">)</span><span class="w"></span>
10541054
</pre></div>
@@ -1079,16 +1079,22 @@ <h2>Plain Old JavaScript Objects<a class="headerlink" href="#plain-old-javascrip
10791079
</div>
10801080
<section id="dynamic-typing-don-t-read-this">
10811081
<h3>Dynamic typing: don’t read this!<a class="headerlink" href="#dynamic-typing-don-t-read-this" title="Permalink to this headline">#</a></h3>
1082-
<p>Through the use of the tools we just described above, Fable guarantees you shouldn’t run into nasty bugs (as long as the interface contracts are correct) because all the code will be checked by the compiler. If it does not compile it either means your Python library does not exist or its path is not good or that your F# implementation lacks something. We do rely on Fable on systems that are used 24/7. We know that if it compiles, it means a 99% chance of running without any problems.</p>
1082+
<p>Through the use of the tools we just described above, Fable guarantees you shouldn’t run into nasty bugs (as long as the
1083+
interface contracts are correct) because all the code will be checked by the compiler. If it does not compile it either
1084+
means your Python library does not exist or its path is not good or that your F# implementation lacks something. We do
1085+
rely on Fable on systems that are used 24/7. We know that if it compiles, it means a 99% chance of running without any
1086+
problems.</p>
10831087
<p>Our motto is: “If it compiles, it works!”</p>
1084-
<p>Still, like we stated, <strong>interop is a question of trust</strong>. If you trust your Python code and F# code, then maybe it’s ok to do things together without further checks. Maybe.</p>
1088+
<p>Still, like we stated, <strong>interop is a question of trust</strong>. If you trust your Python code and F# code, then maybe it’s ok
1089+
to do things together without further checks. Maybe.</p>
10851090
<div class="admonition warning">
10861091
<p class="admonition-title">Warning</p>
10871092
<p>Disclaimer: use this at your own risk</p>
10881093
</div>
10891094
<section id="what-is-dynamic-typing">
10901095
<h4>What is dynamic typing?<a class="headerlink" href="#what-is-dynamic-typing" title="Permalink to this headline">#</a></h4>
1091-
<p>Fable.Core.PyInterop implements the F# dynamic operators so you can easily access an object property by name (without static check) as follows:</p>
1096+
<p>Fable.Core.PyInterop implements the F# dynamic operators so you can easily access an object property by name (without
1097+
static check) as follows:</p>
10921098
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="k">open</span><span class="w"> </span><span class="nn">Fable.Core.PyInterop</span><span class="w"></span>
10931099

10941100
<span class="n">printfn</span><span class="w"> </span><span class="s">&quot;Value: %O&quot;</span><span class="w"> </span><span class="n">pyObject</span><span class="o">?</span><span class="n">myProperty</span><span class="w"></span>
@@ -1102,7 +1108,8 @@ <h4>What is dynamic typing?<a class="headerlink" href="#what-is-dynamic-typing"
11021108
<span class="n">pyObject</span><span class="o">?</span><span class="n">myProperty</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="c1">// Assignment is also possible</span><span class="w"></span>
11031109
</pre></div>
11041110
</div>
1105-
<p>When you combine the dynamic operator with application, Fable will destructure tuple arguments as with normal method calls. These operations can also be chained to replicate Python fluent APIs.</p>
1111+
<p>When you combine the dynamic operator with application, Fable will destructure tuple arguments as with normal method
1112+
calls. These operations can also be chained to replicate Python fluent APIs.</p>
11061113
<div class="highlight-fsharp notranslate"><div class="highlight"><pre><span></span><span class="k">let</span><span class="w"> </span><span class="nv">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">pyObject</span><span class="o">?</span><span class="n">myMethod</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span><span class="w"> </span><span class="mi">2</span><span class="o">)</span><span class="w"></span>
11071114
<span class="c1">// Py: py_object.my_method(1, 2)</span><span class="w"></span>
11081115

0 commit comments

Comments
 (0)