SlideShare uma empresa Scribd logo
1 de 24
GROOVY TRAITS
By - Ali Tanwir
Agenda
➔ Traits
➔ Why use Traits?
➔ How to Use Traits
➔ Extending Traits
➔ Multiple Inheritance Conflicts
and Resolutions
➔ Runtime Implementation of
Traits
➔ Chaining
➔ Differences from Java 8 default
methods
➔ Limitations
Groovy 2.3 introduced traits as a new language construct.
Traits are reusable components, basically set of methods or fields that can be implemented by class.
Traits are a structural construct of the language which allow:
★ composition of behaviors
★ runtime implementation of interfaces
★ behavior overriding
★ compatibility with static type checking/compilation
Traits
Why use Traits ?
We all must have heard of problems of multiple inheritance when working
with java and also familiar with the well known situation of what is known
as 'Diamond Problem'. It says that if there are two classes B and C which
are inherited from A, and class D is inherited from both B and C. If there is a
method in A that B and/or C has overridden, and D does not override it,
then which version of the method does D inherit: that of B, or that of C ??
The situation is like we don’t know from which parent class a particular
feature is inherited from if more than one parent class implements the
feature.
So, traits allows the composition of behavior without going into the
“Diamond Inheritance Problem” allowing us to decide which behavior
prevails upon conflict.
Class A
Class B Class C
Class D
Then it can be used like a normal interface using the implements keyword:
How to use Traits
trait FlyingAbility {
String fly() { "I'm flying!" }
}
Declaration of a trait
Declaration of a method inside a trait
class Bird implements FlyingAbility {}
def b = new Bird()
assert b.fly() == "I'm flying!"
Here:
Adds the trait FlyingAbility to
the Bird class capabilities
instantiate a new Bird
the Bird class automatically
gets the behavior of the
FlyingAbility trait
They can be seen as interfaces carrying both default implementations and state.
A trait is defined using the trait keyword.
Extending Traits
❏ Simple Inheritance
Traits may extend another trait, in which case we must use the
extends keyword:
Here:
the Named trait defines a single
name property
the Polite trait extends the Named
trait
Polite adds a new method which has
access to the name property of the
super-trait
the name property is visible from
the Person class implementing Polite
as is the introduce method
trait Named {
String name
}
trait Polite extends Named {
String introduce() { "Hello, I am $name" }
}
class Person implements Polite {}
def p = new Person(name: 'Ali')
assert p.introduce() == 'Hello, I am Ali'
(Continues…)
❏ Multiple Inheritance
Alternatively, a trait may extend multiple traits. In that case, all super traits must be declared in the
implements clause:
Here:
WithId trait defines the id
property
WithName trait defines the
name property
Identified is a trait which
inherits both WithId and
WithName
trait WithId {
Long id
}
trait WithName {
String name
}
trait Identified implements WithId, WithName {}
Multiple Inheritance Conflicts and
Resolutions
❏ Default conflict resolution
It is possible for a class to implement multiple traits. If some trait defines a method with the same
signature as a method in another trait, we have a conflict:
Here:
trait A defines a method named
exec returning a String
trait B defines the very same
method
class C implements both traits
trait A {
String exec() { 'A' }
}
trait B {
String exec() { 'B' }
}
class C implements A,B {}
(Continues…)
In this case, the default behavior is that methods from the last declared trait wins. Here, B is declared
after A so the method from B will be picked up:
def c = new C()
assert c.exec() == 'B'
(Continues…)
❏ User conflict resolution
In case this behavior is not the one we want, we can explicitly choose which method to call using the
Trait.super.foo syntax. In the example above, we can force to choose the method from trait A, by
writing this:
Here:
explicit call of exec from the
trait A
calls the version from A instead
of using the default resolution,
which would be the one from B
class C implements A,B {
String exec() { A.super.exec() }
}
def c = new C()
assert c.exec() == 'A'
Runtime Implementation of Traits
❏ Implementing a Trait at Runtime
Groovy also supports implementing traits dynamically at runtime. It allows to "decorate" an existing
object using a trait. As an example, let’s start with this trait and the following class:
Here:
the Extra trait defines an extra
method
the Something class does not
implement the Extra trait
Something only defines a
method doSomething
trait Extra {
String extra() { "I'm an extra method" }
}
class Something {
String doSomething() { 'Something' }
}
(Continues…)
Then if we do:
def s = new Something()
s.extra()
the call to extra would fail because Something is not implementing Extra.
(Continues…)
It is possible to do it at runtime with the following syntax:
def s = new Something() as Extra
s.extra()
s.doSomething()
Here:
use of the as keyword to coerce an
object to a trait at runtime
then extra can be called on the
object
and doSomething is still callable
(Continues…)
❏ Implementing Multiple Traits at Once
If we need to implement several traits at once, we can use
the withTraits method instead of the as keyword:
trait A { void methodFromA() {} }
trait B { void methodFromB() {} }
class C {}
def c = new C()
c.methodFromA()
c.methodFromB()
def d = c.withTraits A, B
d.methodFromA()
d.methodFromB()
Here:
call to methodFromA will fail because C
doesn’t implement A
call to methodFromB will fail because C
doesn’t implement B
withTrait will wrap c into something which
implements A and B
methodFromA will now pass because d
implements A
methodFromB will now pass because d also
implements B
Chaining
Groovy supports the concept of stackable traits. The idea is to delegate from one trait to the other if the
current trait is not capable of handling a message.
(Continues...)
(Continues...)
Differences from Java 8 Default Methods
In Java 8, interfaces can have default implementations of methods. If a class implements an interface and
does not provide an implementation for a default method, then the implementation from the interface is
chosen.
Traits behave the same but with a major difference: the implementation from the trait is always used if
the class declares the trait in its interface list and that it doesn’t provide an implementation.
This feature can be used to compose behaviors in an very precise way, in case we want to override the
behavior of an already implemented method.
(Continues…)
Consider two classes that extends class A and implements trait T
So even if we have someMethod() already implemented in the super class, but the classes B and C
declares the trait in its interface list, the behavior will be borrowed from the trait implementation.
Limitations
❏ Compatibility with AST transformations
Traits are not officially compatible with AST transformations. Some of them, like @CompileStatic will be
applied on the trait itself (not on implementing classes), while others will apply on both the implementing
class and the trait. There is absolutely no guarantee that an AST transformation will run on a trait as it
does on a regular class, so use it at your own risk!
(Continues...)
❏ Prefix & Postfix Operations
Within traits, prefix and postfix operations are not allowed if they update a field of the trait:
trait Counting {
int x
void inc() {
x++
}
void dec() {
--x
}
}
class Counter implements Counting {}
def c = new Counter()
c.inc()
Here:
x is defined within the trait, postfix
increment is not allowed
x is defined within the trait, prefix
decrement is not allowed
Note:
A workaround is to use the +=
operator instead.
Some topics for Further Reading
➔ Meaning of this in Traits
➔ Overriding Default Methods
➔ Inheritance of State Gotchas, etc.
References
➔ http://docs.groovy-lang.org/next/html/documentation/core-traits.html
➔ http://www.oodlestechnologies.com/blogs/How-to-use-Groovy-
Traits
➔ http://mrhaki.blogspot.in/2014/05/groovy-goodness-
implementing-traits-at.html
➔ http://mrhaki.blogspot.in/2014/05/groovy-goodness-chaining-
traits.html
➔ http://www.slideshare.net/nareshak/designing-with-groovy-traits-
gr8conf-india (GR8 Conference Naresha’s ppt)
Thank You!

Mais conteúdo relacionado

Último

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Último (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 

Destaque

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destaque (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Groovy Traits

  • 1. GROOVY TRAITS By - Ali Tanwir
  • 2. Agenda ➔ Traits ➔ Why use Traits? ➔ How to Use Traits ➔ Extending Traits ➔ Multiple Inheritance Conflicts and Resolutions ➔ Runtime Implementation of Traits ➔ Chaining ➔ Differences from Java 8 default methods ➔ Limitations
  • 3. Groovy 2.3 introduced traits as a new language construct. Traits are reusable components, basically set of methods or fields that can be implemented by class. Traits are a structural construct of the language which allow: ★ composition of behaviors ★ runtime implementation of interfaces ★ behavior overriding ★ compatibility with static type checking/compilation Traits
  • 4. Why use Traits ? We all must have heard of problems of multiple inheritance when working with java and also familiar with the well known situation of what is known as 'Diamond Problem'. It says that if there are two classes B and C which are inherited from A, and class D is inherited from both B and C. If there is a method in A that B and/or C has overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C ?? The situation is like we don’t know from which parent class a particular feature is inherited from if more than one parent class implements the feature. So, traits allows the composition of behavior without going into the “Diamond Inheritance Problem” allowing us to decide which behavior prevails upon conflict. Class A Class B Class C Class D
  • 5. Then it can be used like a normal interface using the implements keyword: How to use Traits trait FlyingAbility { String fly() { "I'm flying!" } } Declaration of a trait Declaration of a method inside a trait class Bird implements FlyingAbility {} def b = new Bird() assert b.fly() == "I'm flying!" Here: Adds the trait FlyingAbility to the Bird class capabilities instantiate a new Bird the Bird class automatically gets the behavior of the FlyingAbility trait They can be seen as interfaces carrying both default implementations and state. A trait is defined using the trait keyword.
  • 6. Extending Traits ❏ Simple Inheritance Traits may extend another trait, in which case we must use the extends keyword: Here: the Named trait defines a single name property the Polite trait extends the Named trait Polite adds a new method which has access to the name property of the super-trait the name property is visible from the Person class implementing Polite as is the introduce method trait Named { String name } trait Polite extends Named { String introduce() { "Hello, I am $name" } } class Person implements Polite {} def p = new Person(name: 'Ali') assert p.introduce() == 'Hello, I am Ali'
  • 7. (Continues…) ❏ Multiple Inheritance Alternatively, a trait may extend multiple traits. In that case, all super traits must be declared in the implements clause: Here: WithId trait defines the id property WithName trait defines the name property Identified is a trait which inherits both WithId and WithName trait WithId { Long id } trait WithName { String name } trait Identified implements WithId, WithName {}
  • 8. Multiple Inheritance Conflicts and Resolutions ❏ Default conflict resolution It is possible for a class to implement multiple traits. If some trait defines a method with the same signature as a method in another trait, we have a conflict: Here: trait A defines a method named exec returning a String trait B defines the very same method class C implements both traits trait A { String exec() { 'A' } } trait B { String exec() { 'B' } } class C implements A,B {}
  • 9. (Continues…) In this case, the default behavior is that methods from the last declared trait wins. Here, B is declared after A so the method from B will be picked up: def c = new C() assert c.exec() == 'B'
  • 10. (Continues…) ❏ User conflict resolution In case this behavior is not the one we want, we can explicitly choose which method to call using the Trait.super.foo syntax. In the example above, we can force to choose the method from trait A, by writing this: Here: explicit call of exec from the trait A calls the version from A instead of using the default resolution, which would be the one from B class C implements A,B { String exec() { A.super.exec() } } def c = new C() assert c.exec() == 'A'
  • 11. Runtime Implementation of Traits ❏ Implementing a Trait at Runtime Groovy also supports implementing traits dynamically at runtime. It allows to "decorate" an existing object using a trait. As an example, let’s start with this trait and the following class: Here: the Extra trait defines an extra method the Something class does not implement the Extra trait Something only defines a method doSomething trait Extra { String extra() { "I'm an extra method" } } class Something { String doSomething() { 'Something' } }
  • 12. (Continues…) Then if we do: def s = new Something() s.extra() the call to extra would fail because Something is not implementing Extra.
  • 13. (Continues…) It is possible to do it at runtime with the following syntax: def s = new Something() as Extra s.extra() s.doSomething() Here: use of the as keyword to coerce an object to a trait at runtime then extra can be called on the object and doSomething is still callable
  • 14. (Continues…) ❏ Implementing Multiple Traits at Once If we need to implement several traits at once, we can use the withTraits method instead of the as keyword: trait A { void methodFromA() {} } trait B { void methodFromB() {} } class C {} def c = new C() c.methodFromA() c.methodFromB() def d = c.withTraits A, B d.methodFromA() d.methodFromB() Here: call to methodFromA will fail because C doesn’t implement A call to methodFromB will fail because C doesn’t implement B withTrait will wrap c into something which implements A and B methodFromA will now pass because d implements A methodFromB will now pass because d also implements B
  • 15. Chaining Groovy supports the concept of stackable traits. The idea is to delegate from one trait to the other if the current trait is not capable of handling a message.
  • 18. Differences from Java 8 Default Methods In Java 8, interfaces can have default implementations of methods. If a class implements an interface and does not provide an implementation for a default method, then the implementation from the interface is chosen. Traits behave the same but with a major difference: the implementation from the trait is always used if the class declares the trait in its interface list and that it doesn’t provide an implementation. This feature can be used to compose behaviors in an very precise way, in case we want to override the behavior of an already implemented method.
  • 19. (Continues…) Consider two classes that extends class A and implements trait T So even if we have someMethod() already implemented in the super class, but the classes B and C declares the trait in its interface list, the behavior will be borrowed from the trait implementation.
  • 20. Limitations ❏ Compatibility with AST transformations Traits are not officially compatible with AST transformations. Some of them, like @CompileStatic will be applied on the trait itself (not on implementing classes), while others will apply on both the implementing class and the trait. There is absolutely no guarantee that an AST transformation will run on a trait as it does on a regular class, so use it at your own risk!
  • 21. (Continues...) ❏ Prefix & Postfix Operations Within traits, prefix and postfix operations are not allowed if they update a field of the trait: trait Counting { int x void inc() { x++ } void dec() { --x } } class Counter implements Counting {} def c = new Counter() c.inc() Here: x is defined within the trait, postfix increment is not allowed x is defined within the trait, prefix decrement is not allowed Note: A workaround is to use the += operator instead.
  • 22. Some topics for Further Reading ➔ Meaning of this in Traits ➔ Overriding Default Methods ➔ Inheritance of State Gotchas, etc.
  • 23. References ➔ http://docs.groovy-lang.org/next/html/documentation/core-traits.html ➔ http://www.oodlestechnologies.com/blogs/How-to-use-Groovy- Traits ➔ http://mrhaki.blogspot.in/2014/05/groovy-goodness- implementing-traits-at.html ➔ http://mrhaki.blogspot.in/2014/05/groovy-goodness-chaining- traits.html ➔ http://www.slideshare.net/nareshak/designing-with-groovy-traits- gr8conf-india (GR8 Conference Naresha’s ppt)