SlideShare uma empresa Scribd logo
1 de 16
Jan 21, 2016
Scala Apologia
Java
 What’s wrong with Java?
 Not designed for highly concurrent programs

The original Thread model was just wrong (it’s been fixed)

Java 5+ helps by including java.util.concurrent
 Verbose

Too much of Thing thing = new Thing();

Too much “boilerplate,” for example, getters and setters
 What’s right with Java?
 Very popular
 Object oriented (mostly), which is important for large projects
 Strong typing (more on this later)
 The fine large library of classes
 The JVM! Platform independent, highly optimized
Scala is like Java, except when it isn’t
 Java is a good language, and Scala is a lot like it
 For each difference, there is a reason--none of the
changes are “just to be different”
 Scala and Java are (almost) completely interoperable
 Call Java from Scala? No problem!
 Call Scala from Java? Some restrictions, but mostly OK.
 Scala compiles to .class files (a lot of them!), and can be run
with either the scala command or the java command
 To understand Scala, it helps to understand the reasons
for the changes, and what it is Scala is trying to
accomplish
Consistency is good
 In Java, every value is an object--unless it’s a primitive
 Numbers and booleans are primitives for reasons of
efficiency, so we have to treat them differently (you can’t
“talk” to a primitive)
 In Scala, all values are objects. Period.
 The compiler turns them into primitives, so no efficiency is
lost (behind the scenes, there are objects like RichInt)
 Java has operators (+, <, ...) and methods, with different
syntax
 In Scala, operators are just methods, and in many cases
you can use either syntax
Type safety is good, verbosity is bad
 Java is statically typed--a variable has a type, and can hold only
values of that type
 You must specify the type of every variable
 Type errors are caught by the compiler, not at runtime--this is a big win
 However, it leads to a lot of typing (pun intended)
 Languages like Ruby and Python don’t make you declare types
 Easier (and more fun) to write programs
 Less fun to debug, especially if you have even slightly complicated types
 Scala is also statically typed, but it uses type inferencing--that is,
it figures out the types, so you don’t have to
 The good news: Less typing, more fun, type errors caught by the compiler
 The bad news: More kinds of error messages to get familiar with
Verbosity
 Java:
 class Person {
private String firstName;
private String lastName;
private int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public void setFirstName(String firstName) { this.firstName = firstName; }
public void String getFirstName() { return this.firstName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public void String getLastName() { return this.lastName; }
public void setAge(int age) { this.age = age; }
public void int getAge() { return this.age; }
}
 Scala:
 class Person(var firstName: String, var lastName: String, var age: Int)
 Source: http://blog.objectmentor.com/articles/2008/08/03/the-seductions-of-scala-part-i
null
 “I call it my billion-dollar mistake. It was the invention of the
null reference in 1965. At that time, I was designing the first
comprehensive type system for references in an object oriented
language (ALGOL W). My goal was to ensure that all use of
references should be absolutely safe, with checking performed
automatically by the compiler. But I couldn't resist the temptation
to put in a null reference, simply because it was so easy to
implement. This has led to innumerable errors, vulnerabilities,
and system crashes, which have probably caused a billion dollars
of pain and damage in the last forty years.”
--Tony Hoare
null in Scala
 In Java, any method that is supposed to return an object could
return null
 Here are your options:

Always check for null

Always put your method calls inside a try...catch

Make sure the method can’t possibly return null

Ignore the problem and depend on luck
 http://www.youtube.com/watch?v=u0-oinyjsk0
 Yes, Scala has null--but only so that it can talk to Java
 In Scala, if a method could return “nothing,” write it to return an
Option object, which is either Some(theObject) or None
 This forces you to use a match statement--but only when one is really
needed!
Referential transparency
 In Scala, variables are really functions
 Huh?
 In Java, if age is a public field of Person, you can say:
david.age = david.age + 1;
but if age is accessed via methods, you would say:
david.setAge(david.getAge() + 1);
 In Scala, if age is a public field of Person, you can say:
david.age = david.age + 1;
but if Person defines methods age and age_=, you would say:
david.age = david.age + 1;
 In other words, if you want to access a piece of data in Scala, you don’t have
to know whether it is computed by a method or held in a simple variable
 This is the principle of uniform access
 Scala won’t let you use parentheses when you call a function with no parameters
Concurrency
 “Concurrency is the new black.”
 Broadly speaking, concurrency can be either:
 Fine-grained: Frequent interactions between threads working
closely together (extremely challenging to get right)
 Coarse-grained: Infrequent interactions between largely
independent sequential processes (much easier to get right)
 Java 5 and 6 provide reasonable support for traditional
fine-grained concurrency
 Scala has total access to the Java API
 Hence, it can do anything Java can do
 And it can do much more (see next slide)
 Scala also has Actors for coarse-grained concurrency
FP? WTF?
 The big nasty problem with concurrency is dealing with shared
state--multiple threads all trying to read and maybe change the
same variables
 If all data were immutable, then any thread could read it any
time, no synchronization, no locks, no problem
 But if your program couldn’t ever change anything, then it
couldn’t ever do anything, right?
 Wrong!
 There is an entire class of programming languages that use only
immutable data, but work just fine: the functional languages
Functional languages
 The best-known functional languages are ML, OCaml,
and Haskell
 Functional languages are regarded as:
 “Ivory tower languages,” used only by academics (mostly
but not entirely true)
 Difficult to learn (mostly true)
 The solution to all concurrent programming problems
everywhere (exaggerated, but not entirely wrong)
 Scala is an “impure” functional language--you can
program functionally, but it isn’t forced upon you
Scala as a functional language
 The hope--my hope, anyway--is that Scala will let people “sneak
up” on functional programming (FP), and gradually learn to use it
 This is how C++ introduced Object-Oriented programming
 Even a little bit of functional programming makes some things a
lot easier
 Meanwhile, Scala has plenty of other attractions
 FP really is a different way of thinking about programming, and
not easy to master...
 ...but...
 Most people that master it, never want to go back
“You can write a Fortran program...”
 There’s a old saying: “You can write a Fortran program in any language.”
 Some people quote this as “You can write a C program...,” but the quote is older
than the C language
 People still say this, but I discovered recently that what they mean by it has
changed (!)
 Old meaning: You can bring your old (Fortran) programming habits into the
new language, writing exactly the same kind of program you would in Fortran,
whether they make sense or not, and just totally ignore the distinctive
character of the new language.
 New meaning: You can write a crappy program in any language.
 Moral: You can “write a Java program in Scala.” That’s okay at first--you
have to start out with what you know, which is Java. After that, you have a
choice: You can (gradually) learn “the Scala way,” or you can keep writing
crappy Scala programs.
Genealogy
Scala
Java
C
C++
Simula
Smalltal
k
Prolog
Erlang
Haskell
ML
Lisp
functional
programming syntax
objects
pattern
matching
Actors
The End

Mais conteúdo relacionado

Mais procurados

Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersDiego Freniche Brito
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNIntroducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNManish Pandit
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using SwiftDiego Freniche Brito
 
Scala the-good-parts
Scala the-good-partsScala the-good-parts
Scala the-good-partsFuqiang Wang
 
10 Things I Hate About Scala
10 Things I Hate About Scala10 Things I Hate About Scala
10 Things I Hate About ScalaMeir Maor
 
Implementation of TypeGraphQL with Apollo Server
Implementation of TypeGraphQL with Apollo ServerImplementation of TypeGraphQL with Apollo Server
Implementation of TypeGraphQL with Apollo ServerFabien Pasquet
 
Sbt, idea and eclipse
Sbt, idea and eclipseSbt, idea and eclipse
Sbt, idea and eclipseMike Slinn
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoMarcus Denker
 
Scala adoption by enterprises
Scala adoption by enterprisesScala adoption by enterprises
Scala adoption by enterprisesMike Slinn
 
Into the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional ProgrammingInto the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional ProgrammingMike Pence
 

Mais procurados (14)

Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
Java, what's next?
Java, what's next?Java, what's next?
Java, what's next?
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGNIntroducing Scala to your Ruby/Java Shop : My experiences at IGN
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Metaprogramming with javascript
Metaprogramming with javascriptMetaprogramming with javascript
Metaprogramming with javascript
 
Scala the-good-parts
Scala the-good-partsScala the-good-parts
Scala the-good-parts
 
ClassJS
ClassJSClassJS
ClassJS
 
10 Things I Hate About Scala
10 Things I Hate About Scala10 Things I Hate About Scala
10 Things I Hate About Scala
 
Implementation of TypeGraphQL with Apollo Server
Implementation of TypeGraphQL with Apollo ServerImplementation of TypeGraphQL with Apollo Server
Implementation of TypeGraphQL with Apollo Server
 
Sbt, idea and eclipse
Sbt, idea and eclipseSbt, idea and eclipse
Sbt, idea and eclipse
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Java script unleashed
Java script unleashedJava script unleashed
Java script unleashed
 
Scala adoption by enterprises
Scala adoption by enterprisesScala adoption by enterprises
Scala adoption by enterprises
 
Into the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional ProgrammingInto the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional Programming
 

Destaque

Huge skyscraper Architectural - structural Design
Huge  skyscraper Architectural - structural  DesignHuge  skyscraper Architectural - structural  Design
Huge skyscraper Architectural - structural DesignDr.Youssef Hammida
 
Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...
Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...
Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...Dr.Youssef Hammida
 
LAS16-305: Smart City Big Data Visualization on 96Boards
LAS16-305: Smart City Big Data Visualization on 96BoardsLAS16-305: Smart City Big Data Visualization on 96Boards
LAS16-305: Smart City Big Data Visualization on 96BoardsLinaro
 
High Rise Building Research Document
High Rise Building Research DocumentHigh Rise Building Research Document
High Rise Building Research DocumentNicholas Socrates
 
Scalaマクロ入門 bizr20170217
Scalaマクロ入門 bizr20170217 Scalaマクロ入門 bizr20170217
Scalaマクロ入門 bizr20170217 dcubeio
 

Destaque (6)

Lotte Tower
Lotte TowerLotte Tower
Lotte Tower
 
Huge skyscraper Architectural - structural Design
Huge  skyscraper Architectural - structural  DesignHuge  skyscraper Architectural - structural  Design
Huge skyscraper Architectural - structural Design
 
Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...
Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...
Tube structural systemsHigh-rise Building & tubular structures الأبنية البرجي...
 
LAS16-305: Smart City Big Data Visualization on 96Boards
LAS16-305: Smart City Big Data Visualization on 96BoardsLAS16-305: Smart City Big Data Visualization on 96Boards
LAS16-305: Smart City Big Data Visualization on 96Boards
 
High Rise Building Research Document
High Rise Building Research DocumentHigh Rise Building Research Document
High Rise Building Research Document
 
Scalaマクロ入門 bizr20170217
Scalaマクロ入門 bizr20170217 Scalaマクロ入門 bizr20170217
Scalaマクロ入門 bizr20170217
 

Semelhante a scala-intro

Learn scala and it's componenents learn it
Learn scala and it's componenents learn itLearn scala and it's componenents learn it
Learn scala and it's componenents learn itsiddharth30121
 
Scala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationScala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationMartin Odersky
 
Java Closures
Java ClosuresJava Closures
Java ClosuresBen Evans
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinayViplav Jain
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to ClojureRenzo Borgatti
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scalafanf42
 
Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"Salesforce Developers
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with GroovyDhaval Dalal
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slidesMartin Odersky
 
Power Up Your Build at Underscore 2018-02
Power Up Your Build at Underscore 2018-02Power Up Your Build at Underscore 2018-02
Power Up Your Build at Underscore 2018-02Omer van Kloeten
 
Languages used by web app development services remotestac x
Languages used by web app development services  remotestac xLanguages used by web app development services  remotestac x
Languages used by web app development services remotestac xRemote Stacx
 
Introduction to Scala language
Introduction to Scala languageIntroduction to Scala language
Introduction to Scala languageAaqib Pervaiz
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation MobileAcademy
 
Sparklife - Life In The Trenches With Spark
Sparklife - Life In The Trenches With SparkSparklife - Life In The Trenches With Spark
Sparklife - Life In The Trenches With SparkIan Pointer
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.brandongulla
 
Scala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuScala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuHavoc Pennington
 

Semelhante a scala-intro (20)

Learn scala and it's componenents learn it
Learn scala and it's componenents learn itLearn scala and it's componenents learn it
Learn scala and it's componenents learn it
 
Scala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentationScala - The Simple Parts, SFScala presentation
Scala - The Simple Parts, SFScala presentation
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
Introduction to Clojure
Introduction to ClojureIntroduction to Clojure
Introduction to Clojure
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"Scala - from "Hello, World" to "Heroku Scale"
Scala - from "Hello, World" to "Heroku Scale"
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slides
 
Flatmap
FlatmapFlatmap
Flatmap
 
Power Up Your Build at Underscore 2018-02
Power Up Your Build at Underscore 2018-02Power Up Your Build at Underscore 2018-02
Power Up Your Build at Underscore 2018-02
 
Stay fresh
Stay freshStay fresh
Stay fresh
 
Languages used by web app development services remotestac x
Languages used by web app development services  remotestac xLanguages used by web app development services  remotestac x
Languages used by web app development services remotestac x
 
Introduction to Scala language
Introduction to Scala languageIntroduction to Scala language
Introduction to Scala language
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Sparklife - Life In The Trenches With Spark
Sparklife - Life In The Trenches With SparkSparklife - Life In The Trenches With Spark
Sparklife - Life In The Trenches With Spark
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.
 
Scala
ScalaScala
Scala
 
Scala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuScala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on Heroku
 
Devoxx
DevoxxDevoxx
Devoxx
 

Mais de Manav Prasad (20)

Experience with mulesoft
Experience with mulesoftExperience with mulesoft
Experience with mulesoft
 
Mulesoftconnectors
MulesoftconnectorsMulesoftconnectors
Mulesoftconnectors
 
Mule and web services
Mule and web servicesMule and web services
Mule and web services
 
Mulesoft cloudhub
Mulesoft cloudhubMulesoft cloudhub
Mulesoft cloudhub
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Jpa
JpaJpa
Jpa
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
Json
Json Json
Json
 
The spring framework
The spring frameworkThe spring framework
The spring framework
 
Rest introduction
Rest introductionRest introduction
Rest introduction
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Junit
JunitJunit
Junit
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
Xpath
XpathXpath
Xpath
 
Xslt
XsltXslt
Xslt
 
Xhtml
XhtmlXhtml
Xhtml
 
Css
CssCss
Css
 
Introduction to html5
Introduction to html5Introduction to html5
Introduction to html5
 
Ajax
AjaxAjax
Ajax
 

Último

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Último (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

scala-intro

  • 2. Java  What’s wrong with Java?  Not designed for highly concurrent programs  The original Thread model was just wrong (it’s been fixed)  Java 5+ helps by including java.util.concurrent  Verbose  Too much of Thing thing = new Thing();  Too much “boilerplate,” for example, getters and setters  What’s right with Java?  Very popular  Object oriented (mostly), which is important for large projects  Strong typing (more on this later)  The fine large library of classes  The JVM! Platform independent, highly optimized
  • 3. Scala is like Java, except when it isn’t  Java is a good language, and Scala is a lot like it  For each difference, there is a reason--none of the changes are “just to be different”  Scala and Java are (almost) completely interoperable  Call Java from Scala? No problem!  Call Scala from Java? Some restrictions, but mostly OK.  Scala compiles to .class files (a lot of them!), and can be run with either the scala command or the java command  To understand Scala, it helps to understand the reasons for the changes, and what it is Scala is trying to accomplish
  • 4. Consistency is good  In Java, every value is an object--unless it’s a primitive  Numbers and booleans are primitives for reasons of efficiency, so we have to treat them differently (you can’t “talk” to a primitive)  In Scala, all values are objects. Period.  The compiler turns them into primitives, so no efficiency is lost (behind the scenes, there are objects like RichInt)  Java has operators (+, <, ...) and methods, with different syntax  In Scala, operators are just methods, and in many cases you can use either syntax
  • 5. Type safety is good, verbosity is bad  Java is statically typed--a variable has a type, and can hold only values of that type  You must specify the type of every variable  Type errors are caught by the compiler, not at runtime--this is a big win  However, it leads to a lot of typing (pun intended)  Languages like Ruby and Python don’t make you declare types  Easier (and more fun) to write programs  Less fun to debug, especially if you have even slightly complicated types  Scala is also statically typed, but it uses type inferencing--that is, it figures out the types, so you don’t have to  The good news: Less typing, more fun, type errors caught by the compiler  The bad news: More kinds of error messages to get familiar with
  • 6. Verbosity  Java:  class Person { private String firstName; private String lastName; private int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public void setFirstName(String firstName) { this.firstName = firstName; } public void String getFirstName() { return this.firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void String getLastName() { return this.lastName; } public void setAge(int age) { this.age = age; } public void int getAge() { return this.age; } }  Scala:  class Person(var firstName: String, var lastName: String, var age: Int)  Source: http://blog.objectmentor.com/articles/2008/08/03/the-seductions-of-scala-part-i
  • 7. null  “I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.” --Tony Hoare
  • 8. null in Scala  In Java, any method that is supposed to return an object could return null  Here are your options:  Always check for null  Always put your method calls inside a try...catch  Make sure the method can’t possibly return null  Ignore the problem and depend on luck  http://www.youtube.com/watch?v=u0-oinyjsk0  Yes, Scala has null--but only so that it can talk to Java  In Scala, if a method could return “nothing,” write it to return an Option object, which is either Some(theObject) or None  This forces you to use a match statement--but only when one is really needed!
  • 9. Referential transparency  In Scala, variables are really functions  Huh?  In Java, if age is a public field of Person, you can say: david.age = david.age + 1; but if age is accessed via methods, you would say: david.setAge(david.getAge() + 1);  In Scala, if age is a public field of Person, you can say: david.age = david.age + 1; but if Person defines methods age and age_=, you would say: david.age = david.age + 1;  In other words, if you want to access a piece of data in Scala, you don’t have to know whether it is computed by a method or held in a simple variable  This is the principle of uniform access  Scala won’t let you use parentheses when you call a function with no parameters
  • 10. Concurrency  “Concurrency is the new black.”  Broadly speaking, concurrency can be either:  Fine-grained: Frequent interactions between threads working closely together (extremely challenging to get right)  Coarse-grained: Infrequent interactions between largely independent sequential processes (much easier to get right)  Java 5 and 6 provide reasonable support for traditional fine-grained concurrency  Scala has total access to the Java API  Hence, it can do anything Java can do  And it can do much more (see next slide)  Scala also has Actors for coarse-grained concurrency
  • 11. FP? WTF?  The big nasty problem with concurrency is dealing with shared state--multiple threads all trying to read and maybe change the same variables  If all data were immutable, then any thread could read it any time, no synchronization, no locks, no problem  But if your program couldn’t ever change anything, then it couldn’t ever do anything, right?  Wrong!  There is an entire class of programming languages that use only immutable data, but work just fine: the functional languages
  • 12. Functional languages  The best-known functional languages are ML, OCaml, and Haskell  Functional languages are regarded as:  “Ivory tower languages,” used only by academics (mostly but not entirely true)  Difficult to learn (mostly true)  The solution to all concurrent programming problems everywhere (exaggerated, but not entirely wrong)  Scala is an “impure” functional language--you can program functionally, but it isn’t forced upon you
  • 13. Scala as a functional language  The hope--my hope, anyway--is that Scala will let people “sneak up” on functional programming (FP), and gradually learn to use it  This is how C++ introduced Object-Oriented programming  Even a little bit of functional programming makes some things a lot easier  Meanwhile, Scala has plenty of other attractions  FP really is a different way of thinking about programming, and not easy to master...  ...but...  Most people that master it, never want to go back
  • 14. “You can write a Fortran program...”  There’s a old saying: “You can write a Fortran program in any language.”  Some people quote this as “You can write a C program...,” but the quote is older than the C language  People still say this, but I discovered recently that what they mean by it has changed (!)  Old meaning: You can bring your old (Fortran) programming habits into the new language, writing exactly the same kind of program you would in Fortran, whether they make sense or not, and just totally ignore the distinctive character of the new language.  New meaning: You can write a crappy program in any language.  Moral: You can “write a Java program in Scala.” That’s okay at first--you have to start out with what you know, which is Java. After that, you have a choice: You can (gradually) learn “the Scala way,” or you can keep writing crappy Scala programs.