Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/Singleton

Singleton

scala.Singleton
final openclassSingleton

Singleton is used by the compiler as a supertype for singleton types. This includes literal types, as they are also singleton types.

scala> object A { val x = 42 }defined object Ascala> implicitly[A.type <:< Singleton]res12: A.type <:< Singleton = generalized constraintscala> implicitly[A.x.type <:< Singleton]res13: A.x.type <:< Singleton = generalized constraintscala> implicitly[42 <:< Singleton]res14: 42 <:< Singleton = generalized constraintscala> implicitly[Int <:< Singleton]^error: Cannot prove that Int <:< Singleton.

Singleton has a special meaning when it appears as an upper bound on a formal type parameter. Normally, type inference in Scala widens singleton types to the underlying non-singleton type. When a type parameter has an explicit upper bound ofSingleton, the compiler infers a singleton type.

scala> def check42[T](x: T)(implicit ev: T =:= 42): T = xcheck42: [T](x: T)(implicit ev: T =:= 42)Tscala> val x1 = check42(42)^error: Cannot prove that Int =:= 42.scala> def singleCheck42[T <: Singleton](x: T)(implicit ev: T =:= 42): T = xsingleCheck42: [T <: Singleton](x: T)(implicit ev: T =:= 42)Tscala> val x2 = singleCheck42(42)x2: Int = 42

See alsoSIP-23 about Literal-based Singleton Types.

Attributes

Graph
Supertypes
classAny

Members list

Type members

Types

typeSelf
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp