38
Your Scala Type System Working for You!

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

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Like This Idea?We have more,

and we're hiring…

[email protected]

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

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/