Dynamic Languages: the Dynamic Languages: the next big thing for the JVM or an evolutionary dead end? an evolutionary dead end? Chris Richardson Author of POJOs in Action Founder of Cloud Tools and Cloud Foundry Chris Richardson Consulting, Inc www.chrisrichardson.net www.chrisrichardson.net
55
Embed
CommunityOneEast 09 - Dynamic Languages: the next big thing for the JVM or an evolutionary dead end?
There is a good reason why dynamic languages such as Groovy and Ruby are getting a lot of attention. They are powerful, expressive languages that enable developers to easily write concise programs. However, not all of their benefits derive from being dynamic. Many important benefits are simply due to modern language design such as the support for closures. Moreover, dynamic languages have some inherent drawbacks. The extremely limited compile-time checking requires developers to write significantly more tests and severely limits how much help an IDE can provide to a developer. In this session, which is aimed at Java developers and architects, you will learn • What it means for a language to be dynamic • Which Groovy features we liked the most • The frustrating side of Groovy development • How Groovy compares to Scala, which is a modern statically typed language that provides many of benefits of Groovy
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Dynamic Languages: the Dynamic Languages: the next big thing for the JVM or an evolutionary dead end?an evolutionary dead end?
Chris RichardsonAuthor of POJOs in Action
Founder of Cloud Tools and Cloud FoundryyChris Richardson Consulting, Inc
www.chrisrichardson.netwww.chrisrichardson.net
Overall presentation goalp g
Dynamic languages have many benefits But
Static languages which have the Static languages, which have the safety net of compile-time
checking can be just as expressive checking, can be just as expressive
Copyright (c) 2009 Chris Richardson. All rights reserved. Slide 24/8/2009
About ChrisAbout Chris• Grew up in England and live in Oakland, CA• Over 20+ years of software development
experience including 12 years of Javaexperience including 12 years of Java• Author of POJOs in Action• Speaker at JavaOne, SpringOne, NFJS,
JavaPolis, Spring Experience, etc.• Chair of the eBIG Java SIG in Oakland • Chair of the eBIG Java SIG in Oakland
(www.ebig.org)• Run the Groovy/Grails meetup
(http://java.meetup.com/161)• Run a consulting and training company that u a co su t g a d t a g co pa y t at
helps organizations reduce development costs and increase effectiveness
• Founder of Cloud Tools, an open-source project for deploying Java applications on Amazon EC2: http://code google com/p/cloudtoolshttp://code.google.com/p/cloudtools
• Founder of a startup that provides outsourced, automated, and Java-centric datacenter management on the cloud: www.cloudfoundry.comy
Copyright (c) 2009 Chris Richardson. All rights reserved. Slide 34/8/2009
Agendag
The fall and rise of dynamic languagesFavorite Groovy featuresThe frustration of using GroovyScala: expressiveness and compile-p ptime checking
Copyright (c) 2009 Chris Richardson. All rights reserved. Slide 44/8/2009
Dynamic vs. static languages
Dynamic (run-time) Static (compile-time)Dynamic (run-time)Types associated with values rather than
Static (compile-time)Types associated with variables
variablesAbility to define new program elements at
Compile-time checking of language elements and types program elements at
runtimeNot a new idea:
elements and types Not a new idea either:
Lisp – 1958!SmallTalk - 1980
Algol 60
Copyright (c) 2009 Chris Richardson. All rights reserved. Slide 5
anObject.someMethod(someArgs)
4/8/2009
LISP/CLOS – an early (late 1980s) dynamic languagey g g
public String calculateRFC2104HMAC(String data, String key) {try {SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF8"),
HMAC_SHA1_ALGORITHM)M M tI t (HMAC SHA1 ALGORITHM)Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM)mac.init(signingKey)byte[] rawHmac = mac.doFinal(data.getBytes())return new String(Base64.encodeBase64(rawHmac))
}}catch (Exception e) {throw new RuntimeException("Failed to generate HMAC : ", e)
}}
Slide 144/8/2009 Copyright (c) 2009 Chris Richardson. All rights reserved.
Slide 164/8/2009 Copyright (c) 2009 Chris Richardson. All rights reserved.
GStringsg
def schemaScript = """DROP SCHEMA IF EXISTS ${schemaSpec.name};CREATE SCHEMA ${schemaSpec.name};
"""
Slide 174/8/2009 Copyright (c) 2009 Chris Richardson. All rights reserved.
Closures
public EC2Server findInstance(String instanceId) {d f fi d {i t Id it i t Id}def server = servers.find {instanceId == it.instanceId}if (server)return server
else throw new RuntimeException( )else throw new RuntimeException(….)}
Simplified collection processing
Slide 184/8/2009 Copyright (c) 2009 Chris Richardson. All rights reserved.
Closures – new control structurespublic class Ssh {
public Object withTunnel(String publicDnsName, int localPort, int remotePort, Closure closure) {p j ( g p , , , ) {SshConnection sshConnection = makeSshConnection(publicDnsName);try {
if liabilities < assets * 1.5 => true O i ti ( I d t B ki ) f lcase Organization(_, Industry.Banking, _, _, _) => false
case Organization(_, _, _, assets, liabilities) if assets > liabilities => truecase _ => false
}
Copyright (c) 2009 Chris Richardson. All rights reserved. Slide 40
}
4/8/2009
XML generationg@Testclass ScalaXmlExampleTest {
@Testdef xmlLiterals() {val now = new Date()val loanRequestor =
new Organization("Community Bank, Inc", Industry.Banking, 10, 10, 5)
val doc = <loanRequest><time>{now.getTime()}</time><requester>{loanRequestor.name}</requester><requester>{loanRequestor.name}</requester><industry>{loanRequestor.industry}</industry><revenue>{loanRequestor.revenue}</revenue><assets>{loanRequestor.assets}</assets><liabilities>{loanRequestor.liabilities}</liabilities>
</l R t></loanRequest>
val docAsString : String = doc.toString()println(docAsString)
}
Copyright (c) 2009 Chris Richardson. All rights reserved. Slide 41
val loanProcessingService = new LoanProcessingServiceImpl()val policy = new LoanApprovalPolicy()
class LoanProcessingServiceImpl extends LoanProcessingService {
def processLoan(organizationName : String ) = {val organization = organizationRepository.findByName(organizationName)if ( li i L A d( i i )) {if (policy.isLoanApproved(organization)) {// process the loan
} else {throw new LoanRequestRejectedException()
….
Copyright (c) 2009 Chris Richardson. All rights reserved. Slide 50