0% found this document useful (0 votes)
28 views5 pages

Duck Typing Godot GDScript Tutorial Ep 19 Godot Tutorials

The document discusses duck typing in programming. Duck typing is a type system used in dynamic languages where an object's type is defined by the methods it implements rather than its class. It explains that in duck typing, you check if a class has a given method rather than its specific type. The document provides an example using Godot GDScript showing how duck typing allows different classes to be passed to a method if they implement the expected method. It also demonstrates adding type safety checks to validate the object type.

Uploaded by

Chris Lewinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Duck Typing Godot GDScript Tutorial Ep 19 Godot Tutorials

The document discusses duck typing in programming. Duck typing is a type system used in dynamic languages where an object's type is defined by the methods it implements rather than its class. It explains that in duck typing, you check if a class has a given method rather than its specific type. The document provides an example using Godot GDScript showing how duck typing allows different classes to be passed to a method if they implement the expected method. It also demonstrates adding type safety checks to validate the object type.

Uploaded by

Chris Lewinsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Duck Typing | Godot GDScript Tutorial | Ep 19 | Godot Tutorials 5/8/23 4:45 PM

Courses / Introduction-To-Gdscript / Godot-Tutorials-Gdscript-19

Duck Typing | Godot GDScript Tutorial | Ep 19

Duck Typing | Godot GDScript Tutorial | Ep 19

PREV EPISODE NEXT EPISODE

Learning Materials
 Article  Resource

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-19 Page 1 of 5
Duck Typing | Godot GDScript Tutorial | Ep 19 | Godot Tutorials 5/8/23 4:45 PM

Duck Typing
In programming, duck typing is a type system used in dynamic programming languages. The type or class of an object is less important
than the method it defines.

With duck typing, you check if a class has a given method or attribute.

Why is it called Duck Typing?


It comes from the saying:

If it walks like a duck and quack likes a duck, then it must be a duck

Basically in the context of the program, as long as a class has the exact name of the function; we neither care what the function
specifically does nor do we care what class the specific function comes from.

The Is Keyword
For duck typing and checking for null values, we will need to use the is keyword.

The is keyword is used to check the data type of a given object and returns a boolean value:

5 is int # true
Frog is Animal # Does the Frog Class inherit from the Animal Class and is not empty?

Example of Duck Typing

# Animal Class

extends Node
class_name Animal

func fly():
print('Animal flies')

# Duck Class

extends Animal
class_name Duck

func fly():
print('this duck flies')

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-19 Page 2 of 5
Duck Typing | Godot GDScript Tutorial | Ep 19 | Godot Tutorials 5/8/23 4:45 PM

# Circle Class

extends Node
class_name Circle

func fly():
print('Circles are flying???')

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-19 Page 3 of 5
Duck Typing | Godot GDScript Tutorial | Ep 19 | Godot Tutorials 5/8/23 4:45 PM

# Node Class

extends Node2D

var animal = Animal.new()


var duck = Duck.new()
var circle = Circle.new()

# No Type Safety
"""
In this case we can pass it in everything and the function call will work
as long as the class object has that specific function name

letItFly(animal)
letItFly(duck)
letItFly(circle)
"""
func letItFly(flyingObject):
flyingObject.fly()

# With Type Safety


"""
But what if we want type safety?
Say we only want Classes and Sub-Classes of the 'Animal' class???
"""
func animalFlies(animalObject: Animal):
animalObject.fly() # comes with auto complete

"""
The problem is that an error will be thrown if the class is a null value

For Example:
var nullObject: Animal # Null Instantiation w/ type safety

In this case nullObject will make it through the animalFlies() method without warning.
The game will crash when a null object tries to call the 'fly()' because it does not exist
on a null object

In this case we have to check that null values are not sneaking through
"""

# Check for the objects casted as Nulls


# var nullObject: Animal # casted as null but will make it through this function regardless
func animalFliesSafely(animalObject: Animal):

# Option 1
if animalObject == null:
print('object/value does not fly')
return

# Option 2
if (animalObject is Animal) == false:
print('Animal Class not part of Inheritance Chain')
return

# Do whatever you want; you're an animal!


print('continue on ;)')
animalObject.fly() # without a null check, throws an error if null

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-19 Page 4 of 5
Duck Typing | Godot GDScript Tutorial | Ep 19 | Godot Tutorials 5/8/23 4:45 PM

Creative Common License

Duck Typing | Godot GDScript Tutorial | Ep 19 video & article by Godot Tutorials is licensed under a Creative Commons Attribution-
ShareAlike 4.0 International License .

Spread the love, share this if it helped you!

 SHARE ON TWITTER  SHARE ON FACEBOOK  SHARE ON REDDIT  SHARE WITH EMAIL

Subscribe to my Newsletter
Join our newsletter and get news in your inbox! We hate spam too, you
won't get any from me :)

 Email... SUBSCRIBE

GODOT TUTORIALS
WEBSITE RESOURCES LEGAL OTHER

Courses Godot Privacy Policy Contact Me

Trello Cookie Policy Website Info

Github Terms & About Me


Conditions
  
Disclaimer

©2021 Godot Tutorials | Website Powered by Hugo Framework

https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-19 Page 5 of 5

You might also like