Allan Caine
1 min readMay 17, 2018

--

Assume that we want all toasts to be LENGTH_SHORT. Two toast extensions functions of Context are enough.

fun Context.makeToast(message: String) 
= Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

fun Context.makeToast(@StringRes message: Int)
= Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

A Context is related to UIs. Toast extension functions are extension functions of Context only. Extension functions of String are related to operations on strings. Extension functions of Int related to operations on integers. Strings and integers do not operate on toasts.

Extension functions are organized around the classes they extend. So, a file named ContextExt.kt would define the two makeToast extension functions illustrated above.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Allan Caine
Allan Caine

Written by Allan Caine

Senior Android Developer, TD Bank (Canada) (Opinions are my own)

No responses yet

Write a response