{%hackmd _6gkK4EVRrijGnUnS71_YA %}
Julia v1.6 で プロパティ分割代入したい
===
<!-- .element: style="font-size: 300%" -->
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
2021/12/19 JuliaTokai × jl.dev 年末LT大会
antimon2(後藤 俊介)
Note:
LTスライドですっ
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
## お前誰よ?(簡易版)
<!-- .element: style="font-size:60%" -->
+ ![Twitter](https://i.imgur.com/HqouMIg.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [@antimon2](https://twitter.com/antimon2)
+ ![Facebook](https://i.imgur.com/01nPd37.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [antimon2](https://www.facebook.com/antimon2)
+ ![Github](https://i.imgur.com/yBKtii5.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [antimon2](https://github.com/antimon2/)
+ ![Qiita](https://i.imgur.com/FxHMi64.png)<!-- .element: class="plain" style="vertical-align:middle;background:transparent" --> [@antimon2](http://qiita.com/antimon2)
+ [<i class="fa fa-file-text"><!-- .element style="font-size:120%" --></i> @antimon2](https://hackmd.io/@antimon2)
Note:
っ
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
# ~~Julia とは?~~(略)
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
# プロパティ分割代入 とは?
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
```julia
julia> VERSION
v"1.7.0"
julia> nt = (a=1, b=2)
(a = 1, b = 2)
julia> (; a, b) = nt
(a = 1, b = 2)
julia> a
1
julia> b
2
```
出展:[Julia 1.7 Highlights](https://julialang.org/blog/2021/11/julia-1.7-highlights/#property_destructuring)
<!-- .element: style="font-size:67%" -->
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
## プロパティ分割代入 (1)
+ Julia v1.7 の新機能!
+ オブジェクトのプロパティを変数として抽出できる!
+ `(; a, b) = nt` は `a = nt.a; b = nt.b` と等価
+ NamedTuple や複合型など「プロパティ参照」できるものなら何でもOK
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
```julia
julia> myreal((; re)::Complex) = re
myreal (generic function with 1 method)
julia> myreal(2 + 3im)
2
julia> for (; den::UInt8, num::UInt8) in [1//2, 0x03//0x30]
@show num, den
end
(num, den) = (0x01, 0x02)
(num, den) = (0x01, 0x10)
```
一部出展:[Julia 1.7 Highlights](https://julialang.org/blog/2021/11/julia-1.7-highlights/#property_destructuring)
<!-- .element: style="font-size:67%" -->
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
## プロパティ分割代入 (2)
+ 一部プロパティだけの抽出もOK
+ 関数の引数や `for` の左辺もOK
+ 型アノテーションを付けることもできる
+ 順不同
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
便利♪
<!-- .element: style="font-size:667%" -->
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
LTS である v1.6.x でも使いたい…
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
使えるようにした!
<!-- .element: style="font-size:367%" -->
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
[![PropertyDestructuring.jl](https://hackmd.io/_uploads/HyYu-NDqK.png)](https://github.com/antimon2/PropertyDestructuring.jl)
https://github.com/antimon2/PropertyDestructuring.jl
<!-- .element: style="font-size:67%" -->
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
```julia
(@v1.6) pkg> add https://github.com/antimon2/PropertyDestructuring.jl.git
Updating git-repo `https://github.com/antimon2/PropertyDestructuring.jl.git`
Resolving package versions...
Updating `~/.julia/environments/v1.6/Project.toml`
[2286abf2] + PropertyDestructuring v0.1.0 `https://github.com/antimon2/PropertyDestructuring.jl.git#main`
Updating `~/.julia/environments/v1.6/Manifest.toml`
[2286abf2] + PropertyDestructuring v0.1.0 `https://github.com/antimon2/PropertyDestructuring.jl.git#main`
```
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
```julia
julia> using PropertyDestructuring
julia> nt = (a=1, b=2)
(a = 1, b = 2)
julia> @destructure (; a, b) = nt
(a = 1, b = 2)
julia> a
1
julia> b
2
```
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
```julia
julia> @destructure myreal((; re)::Complex) = re
myreal (generic function with 1 method)
julia> myreal(2 + 3im)
2
julia> @destructure for (; den::UInt8, num::UInt8) in [1//2, 0x03//0x30]
@show num, den
end
(num, den) = (0x01, 0x02)
(num, den) = (0x01, 0x10)
```
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
## PropertyDestructuring.jl
+ 式の前に `@destructure` を付けるとその式の中のプロパティ分割代入を展開して処理してくれる!
+ v1.7 でできる大抵のプロパティ分割代入の書式には対応してる!(つもり)
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
### 難点
+ v1.6.x でしか意味がない!
+ v1.7 以降でもエラーは出ないが本来の(組み込みの)挙動に処理スルーしてるだけw
+ 無理矢理対応しているので動作が遅い!
+ 要パフォーマンスチューニング
+ あとまだ不具合や対応漏れあるかも…。
Note:
それでも需要ありそうなら
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
### 展望
+ 需要ありそうなら公式パッケージ化
+ (の前にドキュメント整備)
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
# まとめ
----
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
+ PropertyDestructuring 便利♪
+ PropertyDestructuring 使えると楽しい!
+ Julia 楽しい!
Note:
っ
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
ご清聴ありがとうございます。
Note:
ご清聴ありがとうございますっ!
---
<!-- .slide: data-background-color="rgba(102,130,223,0.3)" -->
# APPENDIX
## リンク
+ [PropertyDestructuring.jl](https://github.com/antimon2/PropertyDestructuring.jl)
+ [Julia 1.7 Highlights](https://julialang.org/blog/2021/11/julia-1.7-highlights)
+ [Property Destructuring](https://julialang.org/blog/2021/11/julia-1.7-highlights/#property_destructuring)
{"metaMigratedAt":"2023-06-16T16:21:50.854Z","metaMigratedFrom":"YAML","title":"Julia v1.6 で プロパティ分割代入したい","breaks":"true","slideOptions":"{\"transition\":\"slide\",\"theme\":\"league\"}","contributors":"[{\"id\":\"80062a4b-8dad-49ac-95bf-848ce0686e9e\",\"add\":6114,\"del\":368}]"}