your scala type system - lightbend...we can do better. type parameters are part of the type! e.g....
TRANSCRIPT
![Page 1: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/1.jpg)
Your Scala Type SystemWorking for You!
![Page 2: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/2.jpg)
The Earth is not FlatBut most of the ways we look at it are:
![Page 3: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/3.jpg)
It's not exactly round either...The Earth has a bit of a "spare-tire"
caused by its rotation
This makes the math harder
Some overly simple models
ignore this
Including one of the most
widely used, Web Mercator
(Google, Bing, etc.)
![Page 4: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/4.jpg)
Referencing a Location on EarthGPS Latitude and Longitude
Defined as 2 angles
One perpendicular to the equator
(with 0 running through London), -180 to 180
One parallel to the poles, -90 to 90
Combined, where they intersect the surface
of the earth defines any point
![Page 5: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/5.jpg)
This is great for points, lines and shapesAs long as they are not too big/coarse
Not very suitable for images though
The problem for images is that
as you move further North or South,
the amount of distance covered by
1.0 degrees longitude changes
At the equator, 1 degree longitude
is about 69 miles, in San Jose it's about
55 miles, and in Anchorage, AK, 33 miles
![Page 6: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/6.jpg)
Two Primary Types of Spatial DataRaster
and
Vector
![Page 7: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/7.jpg)
Raster Images Tend To Assume Constant Measurements
For processing, we usually want
constant measurements for our
pixels (for areas, distances, etc)
This means we need to
mathematically project the 3d
Earth into a 2d flat system
![Page 8: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/8.jpg)
Coordinate Reference Systems and ProjectionsA Coordinate Reference System is anything that assigns coordinates to a location
A projection is anything that mathematically projects between spatial models
The projection (+ datum) defines how one CRS relates to another mathematically
We can convert between the CRS (with varying amounts of error)
Error tends to be smaller for vector data than for raster
Therefore we resist transforming (or resampling) raster data more
![Page 9: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/9.jpg)
Projection ExamplesTransverse Mercator
Conical
Polar
![Page 10: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/10.jpg)
WGS 84 UTM Zones
![Page 11: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/11.jpg)
The Importance of Enforcing CRSMany CRS space coordinates
can overlap
It's critical not to get them
mixed up, or you will
get the wrong data,
sometimes undetectably!
![Page 12: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/12.jpg)
What does most code do?Lots of redundant CRS checks, or they get the wrong CRS…
Works, but it's runtime…
We can do better.
![Page 13: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/13.jpg)
Type Parameters are Part of the Type!E.g. Scala's Set (which is invariant)
![Page 14: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/14.jpg)
This is not a talk on Scala varianceBut here's what you need to know for now: covariant vs invariant
![Page 15: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/15.jpg)
CRS Types! Let's start with a simple definition
You may be wondering why I didn't make this a case object...
![Page 16: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/16.jpg)
We need that case class and its companionto do this…
by doing this...
![Page 17: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/17.jpg)
Type and definition in harmonyWe can now use a type parameter (compile time verified) and still look up the CRS
definition at runtime (so we no longer need to pass it as a regular parameter):
![Page 18: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/18.jpg)
Geometries have the CRS as part of the type
![Page 19: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/19.jpg)
Now, the compiler has your back
![Page 20: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/20.jpg)
I See a Flaw! You Don't Always Know the CRS at Compile Time
![Page 21: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/21.jpg)
Reifying the CRS type from the existential
![Page 22: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/22.jpg)
Other geometries
![Page 23: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/23.jpg)
The API is simple, and hard to get wrong
![Page 24: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/24.jpg)
Adding some more nuance
![Page 25: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/25.jpg)
Now we can limit certain methods
![Page 26: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/26.jpg)
Introducing Features● Features are Geometries with Attributes
● Attributes are fairly loose, and in geotools are untyped (Map[String, Any])
● This leads to all sorts of abuses
● We can do better
● But it would be good to keep a simple "Attach something" capability
![Page 27: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/27.jpg)
Feature Definition
● Geometry is a type parameter, as is CRS
● Attributes is a covariant type parameter
● We can map over the type parameter to obtain a new feature with different
attributes and attribute type, but the same CRS and Geometry
![Page 28: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/28.jpg)
Simple Covariant Behavior
![Page 29: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/29.jpg)
Type-Indexed Maps (TMaps)
![Page 30: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/30.jpg)
Using TMaps
![Page 31: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/31.jpg)
TMap Covariance
![Page 32: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/32.jpg)
TMaps as Feature Attributes
![Page 33: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/33.jpg)
In Use
![Page 34: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/34.jpg)
So why the implicit extension class?
![Page 35: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/35.jpg)
Use =:= instead of <:< ?
● T is Covariant, so <:< must be used, =:= cannot be
● <:< is too loose, so inference fails us
● implicit class RichFeature "fixes" type T first, then addAttr works with inference
![Page 36: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/36.jpg)
Safety Latches
![Page 37: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/37.jpg)
Like This Idea?We have more,
and we're hiring…
http://www.cibotechnologies.com/careers/
![Page 38: Your Scala Type System - lightbend...We can do better. Type Parameters are Part of the Type! E.g. Scala's Set (which is invariant) This is not a talk on Scala variance But here's what](https://reader036.vdocuments.mx/reader036/viewer/2022071102/5fdbe9ea901d59681b1243d2/html5/thumbnails/38.jpg)
And if you would like to brush up on your types (and more)Escalate Software does training, online and in-person:
http://www.escalatesoft.com/training
https://www.udemy.com/user/richard-wall/