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


利用している Google Mobile Ads SDK (play-services-ads) のバージョンを確認する

👉 Google Play と Android の変更点に対応するためのアプリの準備 - Google AdMob ヘルプ hatena-bookmark


implementation com.google.android.gms:play-services-ads:x.y.z

20.4.0 (バージョン)
→ 広告IDの使用を継続するために自動的に権限が宣言されます。

20.5.0
→ オプトアウト済みユーザーのデータ収集と不正防止に対応するため、新しいアプリアセットIDに対応します。

20.6.0
→ tagForChildDirected(TFCD)または tagForUnderAgeOfConsent(TFUA)を通して子供向け取り扱いタグが付与された広告リクエストにおいては、広告IDが送信されないようにすることができます。

👉 Googleモバイル広告SDK  |  Android  |  Google Developers hatena-bookmark

現在、アプリで利用中のバージョンを build.gradle で確認してみたら、


com.google.android.gms:play-services-ads

がありません!

 

依存性を確認していく


./gradlew -q :app:androidDependencies  

...
release
releaseCompileClasspath - Dependencies for compilation
...
+--- com.google.android.play:core:1.10.3@aar
+--- com.google.android.gms:play-services-analytics:18.0.1@aar
+--- com.google.android.gms:play-services-oss-licenses:17.0.0@aar
+--- com.google.android.gms:play-services-tagmanager-v4-impl:18.0.1@aar
+--- com.google.android.gms:play-services-analytics-impl:18.0.1@aar
+--- com.google.firebase:firebase-messaging-ktx:23.0.4@aar
+--- com.google.firebase:firebase-messaging:23.0.4@aar
+--- com.firebaseui:firebase-ui-auth:8.0.1@aar
+--- com.google.android.gms:play-services-auth:19.0.0@aar
+--- com.google.firebase:firebase-ads:20.6.0@aar
+--- com.google.android.gms:play-services-ads:20.6.0@aar 👈
+--- com.google.android.gms:play-services-appset:16.0.0@aar
+--- com.google.firebase:firebase-auth-ktx:21.0.3@aar
+--- com.google.firebase:firebase-auth:21.0.3@aar
+--- com.google.android.gms:play-services-auth-api-phone:17.4.0@aar
+--- com.google.android.gms:play-services-safetynet:17.0.0@aar
+--- com.google.android.gms:play-services-auth-base:17.0.0@aar
+--- com.google.android.gms:play-services-base:18.0.1@aar
...

play-services-ads は、20.6.0 が、なぜか、見えますが。

さらに詳細を確認していきます。


./gradlew -q :app:dependencies --configuration releaseCompileClasspath

...
|         |         +--- com.google.android.gms:play-services-basement:17.0.0 -> 18.0.2 (*)
|         |         \--- com.google.firebase:firebase-annotations:16.0.0
|         \--- com.google.android.gms:play-services-measurement-sdk:21.0.0
|              +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
|              +--- com.google.android.gms:play-services-basement:18.0.0 -> 18.0.2 (*)
|              +--- com.google.android.gms:play-services-measurement-base:21.0.0 (*)
|              \--- com.google.android.gms:play-services-measurement-impl:21.0.0 (*)
+--- com.google.firebase:firebase-ads:20.6.0 👈
|    +--- com.google.android.gms:play-services-ads:20.6.0 👈
|    |    +--- androidx.browser:browser:1.0.0 -> 1.3.0
|    |    |    +--- androidx.core:core:1.1.0 -> 1.7.0 (*)
|    |    |    +--- androidx.annotation:annotation:1.1.0 -> 1.3.0
|    |    |    \--- com.google.guava:listenablefuture:1.0 -> 9999.0-empty-to-avoid-conflict-with-guava
|    |    +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
|    |    +--- androidx.core:core:1.0.0 -> 1.7.0 (*)
|    |    +--- com.google.android.gms:play-services-ads-base:20.6.0
...
(c) - dependency constraint
(*) - dependencies omitted (listed previously)
(n) - Not resolved (configuration is not meant to be resolved)

dependencies の記述の親子依存の調べ方

firebase-ads に依存されていますね。

もう一度、build.gradle を見てみます。


dependencies {
  ...
  implementation 'com.google.firebase:firebase-core:21.0.0'
  implementation 'com.google.firebase:firebase-ads:20.6.0' 👈
  implementation 'com.google.firebase:firebase-analytics-ktx:21.0.0'
  ...

つまりは、

play-services-ads の implementation 記述がなくても

firebase-ads から依存を追って解決されていってたのですね!!