Deep understanding of Scala identifiers, naming and domains

Source: Internet
Author: User

in the Scala , names are used to represent types, values, methods, and classes, which are called entities. Naming is present in local definitions and declarations, inheritance,import clauses, and package clauses, which can be collectively referred to as bindings.

binding has precedence, defined ( Partial or inherited ) has the highest priority, then the explicit Import , followed by a wildcard character Import , and then the package member, which is the lowest priority.

There are two different namespaces, one is the type and the other is the term. The same name can represent a type or term, which depends on the context in which the named app resides.

A binding has a domain that can be accessed with a simple name for an entity with a single named definition in this domain. Domains can be nested. Bindings in the internal domain will obscure the low-priority bindings in the same domain, or the lower-priority or same-priority bindings in the external domain.

Note that cloaking is just a partial order relationship. In the following scenario:

Val x = 1;

{ import p.x;

X

x the bindings do not cover each other. Therefore , the meaning of the reference to x in the third row will be ambiguous .

to an unqualified (type or term) identifier x references can be single-bound under the following conditions:

L Define an entity as an identifier in the same namespace as the name x

l obscures all other entity bindings that define the named X in this namespace

If there is no such binding will cause an error. If x by a import clause binding, then simply name x import clause mapping qualified naming. If x bound by a definition or declaration, then x x

Example 2.0.2 The following is the package P and the Q the two are named X the definition of the object:

Package P {

Object x { val x = 1; val y = 2}

}

Package Q {

Object x { val x = true; val y = ""}

}

The following procedures indicate the different bindings and priorities between them.

Package P {//? X? by Package clause binding

Import console._//?println? by a wildcard character Import binding

Object A {

println ("L4:" +x)// Here's ? X? means ? P.x?

Object B {

Import q._{//? X? by a wildcard character Import binding

println ("L7:" +x)// Here's ? X? means ? Q.x?

import x._//?x? ?y? import binding

println ("L8:" +x)// Here's ? x? means ? Q.x.x?

Object C {

Val x = 3//?x? bound by a local definition

println ("L12:" +x)// Here's ? x? Reference Constants ? 3?

{ import q.x._//?x? and the ? y? by a wildcard character Import binding

//println ("L14:" +x)// here to ? x? refers to ambiguous references

Import x.y//?y? by an explicit Import binding

println ("L16:" +y)// Here's ? y? means ? Q.x.y?

{ val x = "abc"//?x? bound by a local definition

Import p.x._//?x? and the ? y? by a wildcard character Import binding

//println ("L19:" +y)// here to ? y? refers to ambiguous references

println ("L20:" +x)// Here's ? x? refers to a string "ABC"

}}}}}}

A to-qualified(type or term)identifierse.xrefers to a reference in the same namespaceeof typeTa name for thexmember as the identifier. IfTnot a value type will cause an error. e.xis the type of the referenced entityTthe type of the member.

Deep understanding of Scala identifiers, naming and domains

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.