Scala Reserved Words

本文详细列举了Scala语言中的保留关键字,并解释了它们的功能与用法。这些关键字包括但不限于抽象类定义、模式匹配、循环结构等核心语法元素。通过本文,读者可以更好地理解Scala语言的基本构成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Table lists the reserved keywords in Scala. Many are found in Java and they usually have the same meanings in both languages.

WordDescription
abstractMakes a declaration abstract.
caseStart a case clause in a match expression. Define a “case class.”
catchStart a clause for catching thrown exceptions.
classStart a class declaration
defStart a method declaration.
doStart a do…while loop.
elseStart an else clause for an if clause.
extendsIndicates that the class or trait that follows is the parent type of the class or trait being declared.
falseBoolean false.
finalApplied to a class or trait to prohibit deriving child types from it. Applied to a member to prohibit overriding it in a derived class or trait.
finallyStart a clause that is executed after the corresponding try clause, whether or not an exception is thrown by the try clause.
forStart a for comprehension (loop).
forSomeUsed in existential type declarations to constrain the allowed concrete types that can be used.
ifStart an if clause.
implicitMarks a method or value as eligible to be used as an implicit type converter or value. Marks a method parameter as optional, as long as a type-compatible substitute object is in the scope where the method is called.
importImport one or more types or members of types into the current scope.
lazyDefer evaluation of a val.
matchStart a pattern-matching clause.
newCreate a new instance of a class.
nullValue of a reference variable that has not been assigned a value.
objectStart a singleton declaration: a class with only one instance.
overrideOverride a concrete member of a type, as long as the original is not marked final.
packageStart a package scope declaration.
privateRestrict visibility of a declaration.
protectedRestrict visibility of a declaration.
requiresDeprecated. Was used for self-typing.
returnReturn from a function.
sealedApplied to a parent type. Requires all derived types to be declared in the same source file.
superAnalogous to this, but binds to the parent type.
thisHow an object refers to itself. The method name for auxiliary constructors.
throwThrow an exception.
traitA mixin module that adds additional state and behavior to an instance of a class. Can also be used to just declare methods, but not define them, like a Java interface.
tryStart a block that may throw an exception.
trueBoolean true.
typeStart a type declaration.
valStart a read-only “variable” declaration.
varStart a read-write variable declaration.
whileStart a while loop.
withInclude the trait that follows in the class being declared or the object being instantiated.
yieldReturn an element in a for comprehension that becomes part of a sequence.
_A placeholder, used in imports, function literals, etc.
:Separator between identifiers and type annotations.
=Assignment.
=>Used in function literals to separate the argument list from the function body.
<-Used in for comprehensions in generator expressions.
<:Used in parameterized and abstract type declarations to constrain the allowed types.
<%Used in parameterized and abstract type “view bounds” declarations.
>:Used in parameterized and abstract type declarations to constrain the allowed types.
#Used in type projections.
@Marks an annotation.
(Unicode \u21D2) Same as =>.
(Unicode \u2192) Same as ->.
(Unicode \u2190) Same as <-.



Note:
1. break and continue are not listed. These control keywords don’t exist in Scala.
2. Some Java methods use names that are reserved words by Scala, for example, java.util.Scanner.match. To avoid a compilation error, surround the name with single back quotes (“back ticks”), e.g., java.util.Scanner.`match`.


Ref

《Programming Scala》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值