Skip to content

Implicit classes for by-name value classes should not be reported #774

@halotukozak

Description

@halotukozak

Intro

Analyser requires implicit classes to extend AnyVal (in order to decrease memory overhead).

implicit final class IntOps(i: Int) //compilation error: [AVS] Implicit classes should always extend AnyVal to become value classes

It is a valid error also for by-name parameters, becauseval parameters may not be call-by-name, so this should be translated to implicit def conversion + value class, like this:

implicit final class LazyIntOps(i: => Int) //compilation error: [AVS] Implicit classes should always extend AnyVal to become value classes

~>

implicit def toLazyIntOps(i: => Int) = new LazyIntOps(() => i)
final class LazyIntOps(val i: () => Int)

But, it has no meaning for by-name value parameters, since they cannot be wrapped into a value class, so such a translation would be only a unnecessary boilerplate.

Problem

Current implementation of implicitValueClasses rules do not require implicit classes with value parameter to be value classes, because value class may not wrap another user-defined value class, but triggers false positives for by-name ones, e.g.

final class ValueClass(val underlying: Int) extends AnyVal
implicit final class LazyValueClassOps(lvc: => ValueClass) // compilation error: Implicit classes should always extend AnyVal to become value classes

Possible solution

While checking the type of implicit class's parameter, the TypeRepr should be widened by-name (or its scala 2 equivalent)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions