Skip to content

Instantly share code, notes, and snippets.

@Baccata
Created April 19, 2018 12:31
Show Gist options
  • Save Baccata/d876c2de07e3221a3a036880a37b93c2 to your computer and use it in GitHub Desktop.
Save Baccata/d876c2de07e3221a3a036880a37b93c2 to your computer and use it in GitHub Desktop.
An option that forces the developer to ascribe the type to discard it
/**
* Function that allows values to be discarded in a visible way
*
* Thanks https://github.com/tabdulradi for the Trick
*/
object ValueDiscard {
/**
* Function that allows values to be discarded in a visible way.
*
* @tparam T type of the value that will be computed (it won't be inferred,
* must be specified)
* @return function accepting value expression that needs to be computed and
* whose value will be discarded
*/
def apply[T]: (=> T) => Unit = { value =>
val _ = value
}
/**
* This is not best practice. Developers who discard values should be asked
* to exhibit they understand what happens by writing explicitly the type
* of the discarded value.
*/
def badDiscard[T](value: T): Unit = { val _ = value }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment