Skip to content
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

RULE PROPOSAL: Relax parameters to supertype #25

Open
diesalbla opened this issue May 26, 2019 · 2 comments
Open

RULE PROPOSAL: Relax parameters to supertype #25

diesalbla opened this issue May 26, 2019 · 2 comments
Labels

Comments

@diesalbla
Copy link

diesalbla commented May 26, 2019

The goal here is to have a rule to relax the requirements on input method parameters, whenever the extra features of a subtype are not used, and to generalise them to the super-type.

Problem Statement

Suppose the following hold:

  • Ann is a trait or class, that declares some public methods, and Bob is another trait or class that extends from Ann.
  • We have trait Fili, which is not related by subtyping to Ann or Bob, which defines a method def kili(edo:: Bob) that takes as input parameter a value edo of type Bob.
  • Suppose that all the implementations (and overriding ones) of kili only uses from edo the methods or values defined in the super-type Ann, and nowhere does it use the extra capabilities of the Bob class.

The goal, then, is to rewrite the kili method from the Fili trait, so that the type of edo is changed to Ann instead of Bob.

trait Fili {
  def kili(edo: Bob): Int
}
// <<<< Before 
// >>>> After
trait Fili {
  def kili(edo: Ann): Int
}

Comments

Some "proverbs" of engineering, to which this rule help, are those of programming against an interface, not an implementation; as well as the old minimum access.

Examples

One circumstance in which this often happens is in the cats library, and projects that use its type-classes (which are just traits of a higuer kind). It often happens that an implicit parameter is declared to require a Monad[F] parameter, when only the map method (from the Functor trait) is used.

Here are some examples from the same set of related libraries:

@diesalbla diesalbla changed the title PROPOSAL: Rule to relax type requirements on input parameters RULE PROPOSAL: Relax type of parameters to super-type May 26, 2019
@diesalbla diesalbla changed the title RULE PROPOSAL: Relax type of parameters to super-type RULE PROPOSAL: Relax parameters to supertype May 26, 2019
@rorygraves
Copy link
Owner

Great idea

@rorygraves rorygraves added the Rule label Jun 5, 2019
@Daenyth
Copy link

Daenyth commented Jun 24, 2019

This will need some controls: An example being cats-effect Bracket vs Sync. You might be using the "lower" interface because of the additional laws, which aren't represented in code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants