Type class pattern: simulacrum boosted method not found












0















I have a requirement to be able to count number of times AtomicReference[V].get is called in a class that has as field an array of wildcarded atomic references.



To that end, first, I've extended java's AtomicReference[V]:



import java.util.concurrent.atomic.{AtomicInteger => AInt, AtomicReference => ARef}

class MyAtomicReference[V] extends ARef[V]{

private val getCounter: AInt = new AInt(0)

def getAndListen(): V = {
getCounter.getAndIncrement()
super.get()
}

def counter(): Int = getCounter.get()

def resetCounter(): Unit = getCounter.set(0)
}


Then I've added trait AtomicRefCounter which declares the method that I would wish to invoke:



import simulacrum.typeclass

@typeclass trait AtomicRefCounter [R[_], T] {
def countGets(container: R[T]): Int
}


Lastly, I've defined a default AtomicArrayRefCounter in the object DefaultAtomicRefCounters:



object DefaultAtomicRefCounters {

implicit val arrayOfAtomicsTraverser = new AtomicRefCounter[Array, MyAtomicReference[_]] {
override def countGets(container: Array[MyAtomicReference[_]]): Int = container map(_.counter()) sum
}
}


Despite that when I try to call the traverseAtomics() on a corresponding array in a test, I do not see it (I am using Intellij IDEA):



behavior of "removeO1"

"method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
pool.buckets.countGet
}


A piece of advice on what I am missing would really help. Usage of simulacrum is not mandatory - if you feel you know how to solve this without it, I would love to hear that.



update:



This is how the buckets are implemented:



class Pool[T] {

type TimeStampedList = (List[T], Long)

val parallelism: Int = Runtime.getRuntime.availableProcessors * 32
val buckets = new Array[MyAtomicReference[TimeStampedList]](parallelism)
...









share|improve this question

























  • 1. You named the function countGets but call countGet, which is probably just a typo in the question? 2. How is pool.buckets defined/initialized?

    – Sascha Kolberg
    Nov 26 '18 at 5:12











  • Hi Sascha, I've updated the ticket to include definition and initialization of buckets. countGet is not a typo, usually when you type a method in Intellij IDEA it prompts you with a matching member, but I assure you that countGets is not there neither.

    – vasigorc
    Nov 26 '18 at 13:45
















0















I have a requirement to be able to count number of times AtomicReference[V].get is called in a class that has as field an array of wildcarded atomic references.



To that end, first, I've extended java's AtomicReference[V]:



import java.util.concurrent.atomic.{AtomicInteger => AInt, AtomicReference => ARef}

class MyAtomicReference[V] extends ARef[V]{

private val getCounter: AInt = new AInt(0)

def getAndListen(): V = {
getCounter.getAndIncrement()
super.get()
}

def counter(): Int = getCounter.get()

def resetCounter(): Unit = getCounter.set(0)
}


Then I've added trait AtomicRefCounter which declares the method that I would wish to invoke:



import simulacrum.typeclass

@typeclass trait AtomicRefCounter [R[_], T] {
def countGets(container: R[T]): Int
}


Lastly, I've defined a default AtomicArrayRefCounter in the object DefaultAtomicRefCounters:



object DefaultAtomicRefCounters {

implicit val arrayOfAtomicsTraverser = new AtomicRefCounter[Array, MyAtomicReference[_]] {
override def countGets(container: Array[MyAtomicReference[_]]): Int = container map(_.counter()) sum
}
}


Despite that when I try to call the traverseAtomics() on a corresponding array in a test, I do not see it (I am using Intellij IDEA):



behavior of "removeO1"

"method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
pool.buckets.countGet
}


A piece of advice on what I am missing would really help. Usage of simulacrum is not mandatory - if you feel you know how to solve this without it, I would love to hear that.



update:



This is how the buckets are implemented:



class Pool[T] {

type TimeStampedList = (List[T], Long)

val parallelism: Int = Runtime.getRuntime.availableProcessors * 32
val buckets = new Array[MyAtomicReference[TimeStampedList]](parallelism)
...









share|improve this question

























  • 1. You named the function countGets but call countGet, which is probably just a typo in the question? 2. How is pool.buckets defined/initialized?

    – Sascha Kolberg
    Nov 26 '18 at 5:12











  • Hi Sascha, I've updated the ticket to include definition and initialization of buckets. countGet is not a typo, usually when you type a method in Intellij IDEA it prompts you with a matching member, but I assure you that countGets is not there neither.

    – vasigorc
    Nov 26 '18 at 13:45














0












0








0


1






I have a requirement to be able to count number of times AtomicReference[V].get is called in a class that has as field an array of wildcarded atomic references.



To that end, first, I've extended java's AtomicReference[V]:



import java.util.concurrent.atomic.{AtomicInteger => AInt, AtomicReference => ARef}

class MyAtomicReference[V] extends ARef[V]{

private val getCounter: AInt = new AInt(0)

def getAndListen(): V = {
getCounter.getAndIncrement()
super.get()
}

def counter(): Int = getCounter.get()

def resetCounter(): Unit = getCounter.set(0)
}


Then I've added trait AtomicRefCounter which declares the method that I would wish to invoke:



import simulacrum.typeclass

@typeclass trait AtomicRefCounter [R[_], T] {
def countGets(container: R[T]): Int
}


Lastly, I've defined a default AtomicArrayRefCounter in the object DefaultAtomicRefCounters:



object DefaultAtomicRefCounters {

implicit val arrayOfAtomicsTraverser = new AtomicRefCounter[Array, MyAtomicReference[_]] {
override def countGets(container: Array[MyAtomicReference[_]]): Int = container map(_.counter()) sum
}
}


Despite that when I try to call the traverseAtomics() on a corresponding array in a test, I do not see it (I am using Intellij IDEA):



behavior of "removeO1"

"method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
pool.buckets.countGet
}


A piece of advice on what I am missing would really help. Usage of simulacrum is not mandatory - if you feel you know how to solve this without it, I would love to hear that.



update:



This is how the buckets are implemented:



class Pool[T] {

type TimeStampedList = (List[T], Long)

val parallelism: Int = Runtime.getRuntime.availableProcessors * 32
val buckets = new Array[MyAtomicReference[TimeStampedList]](parallelism)
...









share|improve this question
















I have a requirement to be able to count number of times AtomicReference[V].get is called in a class that has as field an array of wildcarded atomic references.



To that end, first, I've extended java's AtomicReference[V]:



import java.util.concurrent.atomic.{AtomicInteger => AInt, AtomicReference => ARef}

class MyAtomicReference[V] extends ARef[V]{

private val getCounter: AInt = new AInt(0)

def getAndListen(): V = {
getCounter.getAndIncrement()
super.get()
}

def counter(): Int = getCounter.get()

def resetCounter(): Unit = getCounter.set(0)
}


Then I've added trait AtomicRefCounter which declares the method that I would wish to invoke:



import simulacrum.typeclass

@typeclass trait AtomicRefCounter [R[_], T] {
def countGets(container: R[T]): Int
}


Lastly, I've defined a default AtomicArrayRefCounter in the object DefaultAtomicRefCounters:



object DefaultAtomicRefCounters {

implicit val arrayOfAtomicsTraverser = new AtomicRefCounter[Array, MyAtomicReference[_]] {
override def countGets(container: Array[MyAtomicReference[_]]): Int = container map(_.counter()) sum
}
}


Despite that when I try to call the traverseAtomics() on a corresponding array in a test, I do not see it (I am using Intellij IDEA):



behavior of "removeO1"

"method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
pool.buckets.countGet
}


A piece of advice on what I am missing would really help. Usage of simulacrum is not mandatory - if you feel you know how to solve this without it, I would love to hear that.



update:



This is how the buckets are implemented:



class Pool[T] {

type TimeStampedList = (List[T], Long)

val parallelism: Int = Runtime.getRuntime.availableProcessors * 32
val buckets = new Array[MyAtomicReference[TimeStampedList]](parallelism)
...






scala intellij-idea implicit-conversion typeclass simulacrum






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 13:43







vasigorc

















asked Nov 25 '18 at 23:45









vasigorcvasigorc

161111




161111













  • 1. You named the function countGets but call countGet, which is probably just a typo in the question? 2. How is pool.buckets defined/initialized?

    – Sascha Kolberg
    Nov 26 '18 at 5:12











  • Hi Sascha, I've updated the ticket to include definition and initialization of buckets. countGet is not a typo, usually when you type a method in Intellij IDEA it prompts you with a matching member, but I assure you that countGets is not there neither.

    – vasigorc
    Nov 26 '18 at 13:45



















  • 1. You named the function countGets but call countGet, which is probably just a typo in the question? 2. How is pool.buckets defined/initialized?

    – Sascha Kolberg
    Nov 26 '18 at 5:12











  • Hi Sascha, I've updated the ticket to include definition and initialization of buckets. countGet is not a typo, usually when you type a method in Intellij IDEA it prompts you with a matching member, but I assure you that countGets is not there neither.

    – vasigorc
    Nov 26 '18 at 13:45

















1. You named the function countGets but call countGet, which is probably just a typo in the question? 2. How is pool.buckets defined/initialized?

– Sascha Kolberg
Nov 26 '18 at 5:12





1. You named the function countGets but call countGet, which is probably just a typo in the question? 2. How is pool.buckets defined/initialized?

– Sascha Kolberg
Nov 26 '18 at 5:12













Hi Sascha, I've updated the ticket to include definition and initialization of buckets. countGet is not a typo, usually when you type a method in Intellij IDEA it prompts you with a matching member, but I assure you that countGets is not there neither.

– vasigorc
Nov 26 '18 at 13:45





Hi Sascha, I've updated the ticket to include definition and initialization of buckets. countGet is not a typo, usually when you type a method in Intellij IDEA it prompts you with a matching member, but I assure you that countGets is not there neither.

– vasigorc
Nov 26 '18 at 13:45












2 Answers
2






active

oldest

votes


















2














I think, you might have gotten wrong how implicits work.



If I read everything correctly, then in your code



implicitly[AtomicRefCounter[Array, MyAtomicReference[_]]].countGets(pool.buckets)


should work.



I you wanted to call countGets on the Array you should use the EnrichMyLibrary pattern.



object DefaultAtomicRefCounters {
implicit class RichArray(private underlying: Array[MyAtomicReference[_]] extends AnyVal {
def countGets: Int = underlying.map(_.counter()).sum
}
}





share|improve this answer































    0














    As disappointing as it is, I couldn't make it work with simulacrum annotation, so I've followed Sascha's advise. I just modified slightly his second example (I couldn't get it to work with implictly) so it compiles and works:



    object TraditionalConcurrencyHelpers {
    implicit class CountArrayAtomicGetsOps(wrapper: Array[MyAtomicReference[(List[Int], Long)]]) {
    def countGets()(implicit atomicRefCounter: AtomicRefCounter[Array, MyAtomicReference[(List[Int], Long)]]): Int = atomicRefCounter.countGets(wrapper)
    }
    }


    With this I have no problem calling countGets on the array:



      behavior of "removeO1"

    "method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
    import TraditionalConcurrencyHelpers._
    import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
    //call pool.removeO1 (not implemented yet)
    pool.buckets.countGets() shouldEqual 1
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473146%2ftype-class-pattern-simulacrum-boosted-method-not-found%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I think, you might have gotten wrong how implicits work.



      If I read everything correctly, then in your code



      implicitly[AtomicRefCounter[Array, MyAtomicReference[_]]].countGets(pool.buckets)


      should work.



      I you wanted to call countGets on the Array you should use the EnrichMyLibrary pattern.



      object DefaultAtomicRefCounters {
      implicit class RichArray(private underlying: Array[MyAtomicReference[_]] extends AnyVal {
      def countGets: Int = underlying.map(_.counter()).sum
      }
      }





      share|improve this answer




























        2














        I think, you might have gotten wrong how implicits work.



        If I read everything correctly, then in your code



        implicitly[AtomicRefCounter[Array, MyAtomicReference[_]]].countGets(pool.buckets)


        should work.



        I you wanted to call countGets on the Array you should use the EnrichMyLibrary pattern.



        object DefaultAtomicRefCounters {
        implicit class RichArray(private underlying: Array[MyAtomicReference[_]] extends AnyVal {
        def countGets: Int = underlying.map(_.counter()).sum
        }
        }





        share|improve this answer


























          2












          2








          2







          I think, you might have gotten wrong how implicits work.



          If I read everything correctly, then in your code



          implicitly[AtomicRefCounter[Array, MyAtomicReference[_]]].countGets(pool.buckets)


          should work.



          I you wanted to call countGets on the Array you should use the EnrichMyLibrary pattern.



          object DefaultAtomicRefCounters {
          implicit class RichArray(private underlying: Array[MyAtomicReference[_]] extends AnyVal {
          def countGets: Int = underlying.map(_.counter()).sum
          }
          }





          share|improve this answer













          I think, you might have gotten wrong how implicits work.



          If I read everything correctly, then in your code



          implicitly[AtomicRefCounter[Array, MyAtomicReference[_]]].countGets(pool.buckets)


          should work.



          I you wanted to call countGets on the Array you should use the EnrichMyLibrary pattern.



          object DefaultAtomicRefCounters {
          implicit class RichArray(private underlying: Array[MyAtomicReference[_]] extends AnyVal {
          def countGets: Int = underlying.map(_.counter()).sum
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 14:05









          Sascha KolbergSascha Kolberg

          5,86612433




          5,86612433

























              0














              As disappointing as it is, I couldn't make it work with simulacrum annotation, so I've followed Sascha's advise. I just modified slightly his second example (I couldn't get it to work with implictly) so it compiles and works:



              object TraditionalConcurrencyHelpers {
              implicit class CountArrayAtomicGetsOps(wrapper: Array[MyAtomicReference[(List[Int], Long)]]) {
              def countGets()(implicit atomicRefCounter: AtomicRefCounter[Array, MyAtomicReference[(List[Int], Long)]]): Int = atomicRefCounter.countGets(wrapper)
              }
              }


              With this I have no problem calling countGets on the array:



                behavior of "removeO1"

              "method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
              import TraditionalConcurrencyHelpers._
              import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
              //call pool.removeO1 (not implemented yet)
              pool.buckets.countGets() shouldEqual 1
              }





              share|improve this answer




























                0














                As disappointing as it is, I couldn't make it work with simulacrum annotation, so I've followed Sascha's advise. I just modified slightly his second example (I couldn't get it to work with implictly) so it compiles and works:



                object TraditionalConcurrencyHelpers {
                implicit class CountArrayAtomicGetsOps(wrapper: Array[MyAtomicReference[(List[Int], Long)]]) {
                def countGets()(implicit atomicRefCounter: AtomicRefCounter[Array, MyAtomicReference[(List[Int], Long)]]): Int = atomicRefCounter.countGets(wrapper)
                }
                }


                With this I have no problem calling countGets on the array:



                  behavior of "removeO1"

                "method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
                import TraditionalConcurrencyHelpers._
                import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
                //call pool.removeO1 (not implemented yet)
                pool.buckets.countGets() shouldEqual 1
                }





                share|improve this answer


























                  0












                  0








                  0







                  As disappointing as it is, I couldn't make it work with simulacrum annotation, so I've followed Sascha's advise. I just modified slightly his second example (I couldn't get it to work with implictly) so it compiles and works:



                  object TraditionalConcurrencyHelpers {
                  implicit class CountArrayAtomicGetsOps(wrapper: Array[MyAtomicReference[(List[Int], Long)]]) {
                  def countGets()(implicit atomicRefCounter: AtomicRefCounter[Array, MyAtomicReference[(List[Int], Long)]]): Int = atomicRefCounter.countGets(wrapper)
                  }
                  }


                  With this I have no problem calling countGets on the array:



                    behavior of "removeO1"

                  "method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
                  import TraditionalConcurrencyHelpers._
                  import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
                  //call pool.removeO1 (not implemented yet)
                  pool.buckets.countGets() shouldEqual 1
                  }





                  share|improve this answer













                  As disappointing as it is, I couldn't make it work with simulacrum annotation, so I've followed Sascha's advise. I just modified slightly his second example (I couldn't get it to work with implictly) so it compiles and works:



                  object TraditionalConcurrencyHelpers {
                  implicit class CountArrayAtomicGetsOps(wrapper: Array[MyAtomicReference[(List[Int], Long)]]) {
                  def countGets()(implicit atomicRefCounter: AtomicRefCounter[Array, MyAtomicReference[(List[Int], Long)]]): Int = atomicRefCounter.countGets(wrapper)
                  }
                  }


                  With this I have no problem calling countGets on the array:



                    behavior of "removeO1"

                  "method" should "remove an element from the pool with time O(1)" in new IntPoolBuilder {
                  import TraditionalConcurrencyHelpers._
                  import org.learningconcurrency.traditional_concurrency.helpers.DefaultAtomicRefCounters._
                  //call pool.removeO1 (not implemented yet)
                  pool.buckets.countGets() shouldEqual 1
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 27 '18 at 0:05









                  vasigorcvasigorc

                  161111




                  161111






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473146%2ftype-class-pattern-simulacrum-boosted-method-not-found%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Futebolista

                      Feedback on college project

                      Albești (Vaslui)