Scala type system programming is a bit complicated and focused __scala

Source: Internet
Author: User
Tags ord
Package Com.xing.listfile/** * Created by Dengni on 2017/2/12. * Scala type System programming Combat: * 1,scala classes and methods, functions can be generic, in the spark source code can be seen everywhere the generics of classes and methods, in the actual instantiation of the type of specific *, such as spark the most core, most basic, most important abstract data structure Rdd the definition of the Rdd class is generic, almost all of the RDD's definitions are generic, and they do so because Rdd derive a number of subclasses that fit a variety of data sources and business logic operations; * 2, about the limits of type boundaries, divided into upper boundary and bottom
  Bounds: * Upper boundary: A type that expresses a generic type must be of a certain type or subclass of some kind. Syntax is:, a new phenomenon here is to qualify the type; * Bottom boundary: The type that expresses the generic type must be a type or a kind of parent class, the syntax is >:; * 3, view Bounds, you can make some kind of mysterious conversion, your type can be converted into the target type without consciousness, in fact, you can think that the view Bounds is a reinforced supplemental version of the upper and lower bounds, for example, in Sparkcontext this spark  In the core class there is a T-<% writable code, which expresses that * t must be of type writable, but T has direct inheritance from the writable interface, which requires the "implicit" way to implement this function; * 4, T: Classtag, such as the RDD class Rdd[t:classtag in the spark source code, is actually a type conversion system, just not enough type information at compile time, * need to use the JVM's runtime to get the complete class through Run-time information
  Type information, which is very important in spark, because the programming and running of the spark program distinguishes between * driver and executor, only when the complete type information is known at run time. * 5, contravariance and covariance;-T and +t * 6, Conext bounds,t:ordering This syntax must be able to program Ordering[t] this way; * This part of the content in the data development, this part of the content will not use too much, butis to tidy up and understand spark this part of the content is very urgent///5, contravariance and covariance;-T and +t-t: If the contravariant is a subclass, the parent class can also implement class Engineer class Expert extends Engine  ER//-t: contravariant if the subclass can be implemented, the parent class can also implement//class MEETING[-T]//+t covariant if the parent can implement the function then the subclass can also implement is covariant class meeting[+t]//What is generic This is the generic T class Animal[t] (Val species:t) {def getanimal (specie:t): T = species} Class Maximum[t:ordering] (Val x:t, VA L y:t) {def bigger (implicit ord:ordering[t]) = {if (Ord.compare (x, y) > 0) x else y}} object helloscal Atypesystem {def main (args:array[string]): unit = {implicit def dog2person (dog:dog) = new Person (dog.name) v
    Al p = new Person ("Scala") Val w = new Worker ("Spark") val dog = new Dog ("Dahuang") New Club (p,w). comunicate New Club[person] (w,dog). Comunicate//new Club (w,dog). Comunicate//feel person is not necessary I'm going to this is necessary to organize don't understand Val E

    = new Meeting[engineer] Participatemeeting (e) val expert = new Meeting[expert] Participatemeeting (Expert) println (New Maximum ("SCala ", Java"). Bigger)} def participatemeeting (Meeting:meeting[engineer]) {println ("Welcome")}} class Pers On (Val name:string) {def talk (Person:person) {println (this.name + ":" + Person.name)}} class Worker (name:st Ring) extends the person (name) class Dog (Val name:string)//upper and lower bounds there is not much grammatical difference in the strictest sense, except that the line is distinguished from the subordinate relationship of the class.: Is the upper boundary because



The person is the top-level parent, and if it's a worker, that's the definition of the bottom border./*class club[t: Person] (P1:t, p2:t) {def comunicate = P1.talk (p2)}*/


 Class club[t <% person] (P1:t, p2:t) {def comunicate = P1.talk (p2)}



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.