Code Monkey home page Code Monkey logo

Comments (7)

erikvanoosten avatar erikvanoosten commented on August 24, 2024

Wow, that's a pretty impressive finding. Thanks for that!

Also thanks for the 2 suggestions. I'll have to think about which one I prefer. @skullxbones, do you have a preference?

from metrics-scala.

scullxbones avatar scullxbones commented on August 24, 2024

I learned something today, thank you! I didn't realize that by value and implicit interacted this way.

Let me write up that failing test case. Probably making the signature Unit => A is the way to go since it's less magical and more explicit.

from metrics-scala.

erikvanoosten avatar erikvanoosten commented on August 24, 2024

This issue is related: https://issues.scala-lang.org/browse/SI-3237.

from metrics-scala.

som-snytt avatar som-snytt commented on August 24, 2024

FWIW, I think changing the signature of healthCheck to specify by-name semantics is the obvious minimal change. Otherwise, you wouldn't expect this to work either:

  @Test def explicit(): Unit = {
    var count = 0
    var checker = false
    val res = healthCheck("bool test","nope") {
      count += 1 
      new HealthCheckMagnet {
        def apply(unhealthyMessage: String) = () => if (checker) "OK" else unhealthyMessage
      } 
    } 
    assertEquals("nope", res())
    checker = true
    assertEquals("OK", res())
    assertEquals(2, count)
  }

Here is the minimization I was working with.

import language._

package object health {
  type HealthCheck = () => String

  def healthCheck(name: String, unhealthyMessage: String = "Health check failed")(checker: =>HealthCheckMagnet): HealthCheck = {
    () => checker(unhealthyMessage)()
  } 
}     

package health {

  /*sealed*/ trait HealthCheckMagnet {
    def apply(unhealthyMessage: String): HealthCheck
  } 

  object HealthCheckMagnet {

    /** Magnet for checkers returning a [[scala.Boolean]] (possibly implicitly converted).  */
    implicit def fromBooleanCheck[A <% Boolean](checker: => A) = new HealthCheckMagnet {
      def apply(unhealthyMessage: String) = () => if (checker) "OK" else unhealthyMessage
    }   
  }   
}   

with test

  @Test def bools(): Unit = {
    var count = 0
    val res = healthCheck("bool test","nope") {
      count += 1
      false
    }
    assertEquals("nope", res())
    assertEquals("nope", res()) 
    assertEquals(2, count)
  }

The other difference may be that healthCheck is not invoking checker immediately.

Frankly, I'm confused by the checkers and checks, and I lost my scorecard. And am I the only one who keeps thinking "chick magnet"?

from metrics-scala.

erikvanoosten avatar erikvanoosten commented on August 24, 2024

I am rather to old to be a chick magnet, but it sound great nonetheless!

Thanks for the idea of making the parameter by-name also. Your prototype should be very easy to integrate. Am I right that this will break binary compatibility (but keep source compatibility)?

from metrics-scala.

erikvanoosten avatar erikvanoosten commented on August 24, 2024

There is an interesting twist you didn't consider. The (java) class HealthCheck comes from metrics-core and we can't just redefine that.

from metrics-scala.

erikvanoosten avatar erikvanoosten commented on August 24, 2024

Anyway, just declaring the checker as by name was sufficient. Thanks!

from metrics-scala.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.