lampepfl / lampepfl/steps

Should extension methods/parent methods be used vs normal methods?

Open
#31 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
15
Forks
3
PR merge metrics
No merged PRs in 30d

Description

Looking at https://github.com/lampepfl/steps/blob/febe88e2f5db95ea6739aacda955eff3f729d03b/src/main/scala/steps/result/package.scala

It appears that most of the code relies on an enum with extension methods or methods in the parent, vs defining the methods in the child classes.

Won't this potentially limit JVM optimizations? Consider the following example (https://godbolt.org/z/48rqPqE75)

  def example(x: Result[Int, Int]): Iterator[Int] =
    x match
      case xx: Result.Ok[Int]   => xx.iterator
      case _: Result.Err[Int] => 
        println("ERROR")
        Iterator.empty

Note that the call to xx.iterator will call Ok.Iterator directly.
In the current implementation, this will do another instanceof check, which would be avoided if the methods were defined in the child classes.

I understand that this is a micro optimization, but if the plan is to move this to the stdlib, we might as well make sure that it's performant.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/main/scala/steps/result/package.scala and compare the enum extension and parent methods with methods on the child classes. Reproduce or inspect the JVM behavior in the linked Godbolt example, then determine whether the implementation should change for performance. Done means reaching and documenting a clear design decision; no project test is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.