1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

概要

Scala3では、型に対する中置演算子(infix operator)を簡単に定義できます。
これにより、コードが直感的で読みやすくなります。以下に、その方法と使用例を示します。

中置演算子の定義と使用例

以下のコードでは、orという中置演算子を定義し、StringまたはInt型を表現しています。

サンプルコード

scala> infix type or[X, Y] = X | Y
// defined alias type or[X,Y] = X | Y

scala> val x: String or Int = 10
val x: String or Int = 10

scala> val x: String or Int = "str"
val x: String or Int = str

解説

  1. 中置演算子の定義: infix type or[X, Y] = X | Yで、orを中置演算子として定義しています。
  2. 使用例: val x: String or Int = 10val x: String or Int = "str"で、orを使って異なる型の値を持つ変数を定義しています。

このように、中置演算子を使うことで、型の選択をより直感的に表現できます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?