Kotlin スコープ関数 の上手な使い分け その4 - let

kotlin scope function

 

■ let の便利な使い方 (公式)


public inline fun <T, R> T.let(block: (T) -> R): R {
  contract {...}
  return block(this)
}

The Kotlin standard library function let can be used for scoping and null-checks. When called on an object, let executes the given block of code and returns the result of its last expression. The object is accessible inside the block by the reference it (by default) or a custom name.

Kotlinの標準ライブラリ関数 let は、スコープや NULL チェックに使用することができます。オブジェクト上で呼び出されると、let は与えられたコードブロックを実行
し、その最後の式の結果を返します。オブジェクトはブロック内で参照 it (デフォルト) またはカスタム名でアクセスできます。

Executing a lambda on non-null objects

NULLでないオブジェクトに対してラムダを実行する

Introducing an expression as a variable in local scope

ローカルスコープでの変数としての式の導入

Execute if not null

nullでない場合は実行


val value = ...

value?.let {
    ... // execute this block if not null
}

Map nullable value if not null

nullでない場合、nullableな値をマップする


val value = ...

val mapped = value?.let { transformValue(it) } ?: defaultValue
// defaultValue is returned if the value or the transform result is null.

👉 Kotlin Examples: Learn Kotlin Programming By Example hatena-bookmark
👉 Scope functions | Kotlin hatena-bookmark
👉 Idioms | Kotlin hatena-bookmark

 

■ まとめ

公式では、「let」は「null を避けてのブロック内処理」に使うことを推しています。

let 自体が不要な場合は、AndroidStudio や IDE が知らせてくれます。

👉 Kotlin スコープ関数 の上手な使い分け その1 - apply hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その2 - also hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その3 - with hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その4 - let hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その5 - run hatena-bookmark


Kotlin スコープ関数 の上手な使い分け その3 - with

kotlin scope function

 

■ with の便利な使い方 (公式)


public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
  contract {...}
  return receiver.block()
}

with is a non-extension function that can access members of its argument concisely: you can omit the instance name when referring to its members.

with は非拡張関数で、引数のメンバに簡潔にアクセスできる。メンバを参照する際にインスタンス名を省略できる。

Grouping function calls on an object

オブジェクトの関数呼び出しをグループ化する。

Call multiple methods on an object instance (with)

オブジェクトのインスタンスに対して複数のメソッドを呼び出す。

👉 Kotlin Examples: Learn Kotlin Programming By Example hatena-bookmark
👉 Scope functions | Kotlin hatena-bookmark
👉 Idioms | Kotlin hatena-bookmark

 

■ まとめ

「with」は「記述の長い View などの連続操作をまとめる」のに多く使わています。


with(binding) {
  progressBar.visibility = View.VISIBLE
  cancelButton.visibility = View.VISIBLE
  goButton.visibility = View.GONE
  seeFileButton.visibility = View.GONE
}


with(viewBinding.recyclerView) {
  setHasFixedSize(true)
  adapter = ProductSearchAdapter()
  layoutManager =
    LinearLayoutManager(
      this@ProductSearchActivity,
      LinearLayoutManager.VERTICAL,
      false
    )
}


with(holder.binding) {
  person = items[position]
  executePendingBindings()
}

with の戻り値を使わないことのほうが多く見えますが、戻り値はあります。


val windowDpSize = with(LocalDensity.current) {
  windowSize.toDpSize()
}

👉 Kotlin スコープ関数 の上手な使い分け その1 - apply hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その2 - also hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その3 - with hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その4 - let hatena-bookmark
👉 Kotlin スコープ関数 の上手な使い分け その5 - run hatena-bookmark


macOS 12.3 で Python2 が消え、スクリプトが「bad interpreter: /usr/bin/python: no such file or directory」とは。

便利スクリプトツールたちが使えなくなってつらし。


#!/usr/bin/python -u
...

macOS 12.3 で /usr/bin/python は削除され、 /usr/bin/python3 しかないのですが。という件。

👉 `pidcat` installed via `brew` no longer works on macOS 12.3, due to Python 2 having been removed · Issue #180 · JakeWharton/pidcat hatena-bookmark

python 2 をインストールして、そのパスに1行目を書き換える。


#!/usr/bin/env -S python -u


#!/Users/<user>/.pyenv/versions/2.7.18/bin/python -u


#!/usr/local/Cellar/python@2/2.7.15_1/bin/python2 -u

macOS SIP により python から python3ln -s で向けることはできない。

👉 Mac のシステム整合性保護について - Apple サポート (日本) hatena-bookmark

まとめ

pidcat に関しては以下で。


❯ pyenv global 2.7.18

❯ pyenv versions
  system
* 2.7.18 (set by /Users/username/.pyenv/version)
  3.10.4


#!/usr/bin/env -S python -u
...

bad interpreter: /usr/bin/python: no such file or directory

しかし、pyenv て便利なやつだなあ。

👉 pyenv/pyenv: Simple Python version management hatena-bookmark

👉 【Python】「pip search」の代わりは何なの? hatena-bookmark