Skip to content

Control Structures- Description does not match function's signature #2097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Altair-Bueno opened this issue Jun 25, 2021 · 3 comments
Closed

Comments

@Altair-Bueno
Copy link
Contributor

On Control Structures - Using a match expression as the body of a method:

Because match expressions return a value, they can be used as the body of a method.
This method takes a Boolean value as an input parameter, and returns a String, based on the result of the match expression:

def isTruthy(a: Matchable) = a match
 case 0 | "" => false
 case _ => true

This method does not only take Boolean as an input, as stated by the function signature. It should work with any Matchable value. Also, it does not contain the case false neither the case null scenario. I'm not sure if it is intended this way or it is an error. I imagine this function tries to mimic JavaScript's behaviour so it should consider those cases too. I added some spaces to ident the true case as it is usually done in other other FP languages like Haskell. I'm not familiar enough with Scala, let me know if my changes are OK and i'll create a pull request

Proposed solution

Because match expressions return a value, they can be used as the body of a method.
This method takes a Matchable value as an input parameter, and returns a Boolean, based on the result of the match expression:

def isTruthy(a: Matchable) = a match
  case 0 | "" | false | null => false
  case _                     => true

The input parameter a is defined to be the [Matchable type][matchable]---which is the root of all Scala types that pattern matching can be performed on.
The method is implemented by matching on the input, providing two cases:
The first one checks whether the given value is either the integer 0, an empty string, null or false and returns false in this case.
In the default case, we return true for any other value.
These examples show how this method works:

isTruthy(0)      // false
isTruthy(false)  // false
isTruthy(null)   // false
isTruthy("")     // false
isTruthy(1)      // true
isTruthy(" ")    // true
isTruthy(2F)     // true

Using a match expression as the body of a method is a very common use.

@Altair-Bueno
Copy link
Contributor Author

Quick note: isTruthy appears here too. Apparently, this function refers to Perl's definition of truthy. Sadly, i'm not familiar with Perl

@SethTisue
Copy link
Member

yes, a PR on this would be welcome

Altair-Bueno added a commit to Altair-Bueno/docs.scala-lang that referenced this issue Aug 11, 2021
Altair-Bueno added a commit to Altair-Bueno/docs.scala-lang that referenced this issue Aug 11, 2021
@Altair-Bueno
Copy link
Contributor Author

I created a PR using Perl's definition of truthy as seen here to keep things related

julienrf added a commit that referenced this issue Aug 30, 2021
Control Structures- Description does not match function's signature #2097
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants