c# barcode generator library open source TRAITS AND TYPES AND GNARLY STUFF FOR ARCHITECTS in Font

Encode ECC200 in Font TRAITS AND TYPES AND GNARLY STUFF FOR ARCHITECTS

CHAPTER 7 TRAITS AND TYPES AND GNARLY STUFF FOR ARCHITECTS
Data Matrix ECC200 Encoder In None
Using Barcode generation for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
Painting Data Matrix 2d Barcode In None
Using Barcode creator for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
contain a Double, and an Array[Object] can contain String, Integer, and so on. Let s put a Double into a Holder[Number]:
Generate USS Code 39 In None
Using Barcode generator for Font Control to generate, create USS Code 39 image in Font applications.
www.OnBarcode.com
UPC-A Drawer In None
Using Barcode encoder for Font Control to generate, create UPC Code image in Font applications.
www.OnBarcode.com
scala> val nh = new Holder[Number](33.3d)
Encoding QR Code 2d Barcode In None
Using Barcode drawer for Font Control to generate, create Quick Response Code image in Font applications.
www.OnBarcode.com
Make USS Code 128 In None
Using Barcode encoder for Font Control to generate, create Code 128C image in Font applications.
www.OnBarcode.com
nh: Holder[java.lang.Number] = Holder@340c9c
Print Barcode In None
Using Barcode generator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Painting ISSN - 13 In None
Using Barcode encoder for Font Control to generate, create International Standard Serial Number image in Font applications.
www.OnBarcode.com
And we define a method that rounds the number:
Printing ECC200 In None
Using Barcode creator for Software Control to generate, create ECC200 image in Software applications.
www.OnBarcode.com
Generate Data Matrix In .NET
Using Barcode creation for Reporting Service Control to generate, create DataMatrix image in Reporting Service applications.
www.OnBarcode.com
scala> def round(in: Holder[Number]) {in.data = in.data.intValue}
Read Barcode In .NET Framework
Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications.
www.OnBarcode.com
Draw GS1-128 In Java
Using Barcode creation for BIRT reports Control to generate, create EAN128 image in Eclipse BIRT applications.
www.OnBarcode.com
round: (Holder[java.lang.Number])Unit
Recognizing EAN-13 Supplement 5 In .NET Framework
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Make Code-39 In Objective-C
Using Barcode generator for iPad Control to generate, create Code 39 Extended image in iPad applications.
www.OnBarcode.com
We call the round method, and let s see what we get out the other side:
EAN / UCC - 13 Generation In None
Using Barcode generator for Microsoft Word Control to generate, create GTIN - 128 image in Word applications.
www.OnBarcode.com
Barcode Creation In Java
Using Barcode generation for Eclipse BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
scala> round(nh) scala> nh.data
Drawing Matrix In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create 2D image in VS .NET applications.
www.OnBarcode.com
Print EAN-13 In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create EAN 13 image in .NET framework applications.
www.OnBarcode.com
res16: java.lang.Number = 33
QR Decoder In VS .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Paint QR Code In .NET Framework
Using Barcode creator for Visual Studio .NET Control to generate, create Denso QR Bar Code image in .NET applications.
www.OnBarcode.com
We put in a Number and got back a Number. What s the underlying class for the Number
scala> nh.data.getClass
res17: java.lang.Class[_] = class java.lang.Integer
Great. Integer is a subclass of Number, so we can put a Integer or a Double into the Holder[Number]. We preserve the ability to use class hierarchies with invariant type parameters. Let s finally see what happens when we try to pass a Holder[Double] into round.
scala> val dh = new Holder(33.3d)
dh: Holder[Double] = Holder@1801e5f
CHAPTER 7 TRAITS AND TYPES AND GNARLY STUFF FOR ARCHITECTS
scala> round(dh)
<console>:8: error: type mismatch; found : Holder[Double] required: Holder[java.lang.Number]
So, invariant type parameters protect us when we have mutable data structures like arrays. Let s move on to covariant parameter types.
Covariant Parameter Types
Covariant parameter types are designated with a + before the type parameter. A covariant type is useful for read-only containers. Scala s List is defined as List[+T], which means that it s covariant on type T. List is covariant because if you pass a List[String] to a method that expects a List[Any], then every element of the List satisfies the requirement that is an Any and we cannot change the contents of the List. Let s define an immutable class, Getable. Once an instance of Getable is created, it cannot change, so we can mark its type, T, as covariant.
scala> class Getable[+T](val data: T)
defined class Getable
Let s define a method that takes a Getable[Any]:
scala> def get(in: Getable[Any]) {println("It's "+in.data)}
get: (Getable[Any])Unit
We define an instance of Getable[String]:
scala> val gs = new Getable("String")
gs: Getable[java.lang.String] = Getable@10a69f0
CHAPTER 7 TRAITS AND TYPES AND GNARLY STUFF FOR ARCHITECTS
We can call get with gs:
scala> get(gs)
It's String
Let s try the same example but passing a Getable[java.lang.Double] into something that expects a Getable[Number]:
scala> def getNum(in: Getable[Number]) = in.data.intValue
getNum: (Getable[java.lang.Number])Int
scala> def gd = new Getable(new java.lang.Double(33.3))
gd: Getable[java.lang.Double]
scala> getNum(gd)
res7: Int = 33
Yes, the covariance works the way we expect it to. We can make read-only classes covariant. I guess that means that contravariance is good for write-only classes.
Contravariant Parameter Types
So, if covariance allows us to pass List[String] to a method that expects List[Any], what good is contravariance Let s first look at a write-only class, Putable:
scala> class Putable[-T] { def put(in: T) {println("Putting "+in)} }
CHAPTER 7 TRAITS AND TYPES AND GNARLY STUFF FOR ARCHITECTS
Next, let s define a method that takes a Putable[String]:
scala> def writeOnly(in: Putable[String]) {in.put("Hello")}
writeOnly: (Putable[String])Unit
And let s declare an instance of Putable[AnyRef]:
scala> val p = new Putable[AnyRef]
p: Putable[AnyRef] = Putable@75303f
And what happens if we try to call writeOnly
scala> writeOnly(p)
Putting Hello
Okay, so we can call a method that expects a Putable[String] with a Putable[AnyRef] because we are guaranteed to call the put method with a String, which is a subclass of AnyRef. Standing alone, this is not particularly valuable, but if we have a class that does something with input that results in output, the value of contravariance becomes obvious. The inputs to a transformation are contravariant. Calling something that expects at least any AnyRef with a String is legal and valid. But the return value can be covariant because we expect to get back a Number, so if we get an Integer, a subclass of Number, we re okay. Let s see how it works. We ll define DS with a contravariant In type and a covariant Out type:
scala> trait DS[-In, +Out]{def apply(i: In): Out}
defined trait DS
CHAPTER 7 TRAITS AND TYPES AND GNARLY STUFF FOR ARCHITECTS
Let s create an instance that will convert Any into an Int:
scala> val t1 = new DS[Any, Int]{def apply(i: Any) = i.toString.toInt}
t1: java.lang.Object with DS[Any,Int] = $anon$1@14dcfad
We define check, a method that takes a DS[String, Any]:
scala> def check(in: DS[String, Any]) = in("333")
check: (DS[String,Any])Any
And we call check with t1:
scala> check(t1)
res14: Any = 333
Rules of Variance
So, we ve successfully defined and used an invariant type. The invariant type was mutable, so it both returned and was called with a particular type. We created a convariant type which was an immutable holder of a value. Finally, we created a transformer that had contravariant input and covariant output. Wait, that sounds like a function. That s right, Scala s FunctionN traits have contravariant parameters and covariant results. This leads us to the simple rules of variance: Mutable containers should be invariant. Immutable containers should be covariant. Inputs to transformations should be contravariant, and outputs from transformations should be covariant. In very few pages, we ve covered a very complex topic. So, thanks for hanging in. Let s go have some fun watching dragons and other monsters kill bunnies.
Copyright © OnBarcode.com . All rights reserved.