Scala in Depth 6 Scala type system

Source: Internet
Author: User

Answer the previous article.

1. Is there also a syntax for declaring upper bound and Lower Boud in Java? Some, such as:

List<e extends numeric> list<e Upper integer>


What is the difference between list<?> and list<object> in 2.Java?

List<?> represents any type of list that can be assigned to it

List<object> represents the type of element in list is arbitrary

Package Tstge;import java.util.*;p ublic class Tst {public static void main (string[] args) {arraylist<?>        Mylist1 = null;        Arraylist<object> mylist2 = null;        arraylist<integer> mylist3 = new arraylist<integer> (); Mylist2 = Mylist3; error!    Type Mismatch:cannot convert from arraylist<integer> to arraylist<object> mylist1 = Mylist3; }}


The type of 3.Java can usually be translated directly into the Scala type. But like Iterator<?> iterator< Extends component> such a type how to translate it?

Iterator[t] Forsome {type T} or Iterator[_]
Iterator[t] Forsome {type T <: Component}


About Forsome,stack overflow has a very good introduction:

Http://stackoverflow.com/questions/15186520/scala-any-vs-underscore-in-generics


4.View bound

def foo[a <% B] (x:a) = X

This means that where you call the Foo method, you must be able to see the conversion function from type A to type B, which is equivalent to

def Foo[a] (x:a) (implicit $ev 0:a = B) = X


5. Context bound

def Foo[a:b] (x:a) = X

TheFooMethod defines a constraintA:b. This constraint means the parameter
Xis of typeAa nd there must is an implicit value b[a]  available when calling method
foo. Context bounds can be rewritten as follows

def Foo[a] (x:a) (implicit $ev 0:b[a]) = X

The current context must have an implicit Val of type b[a]

Scala in Depth 6 Scala type system

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.