Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 08:42
    mkeskells commented #8824
  • 08:41
    mkeskells synchronize #8824
  • 07:36
    mkeskells review_requested #8825
  • 07:33
    mkeskells review_requested #8841
  • 06:31
    eed3si9n commented #8820
  • 06:25
    eed3si9n edited #8820
  • 05:29
    eed3si9n synchronize #8820
  • 04:13
    eed3si9n synchronize #8820
  • 03:52
    eed3si9n synchronize #8820
  • 02:42
    SethTisue commented #8855
  • 00:47
    Ichoran commented #8856
  • 00:41
    som-snytt commented #8848
  • 00:31
    SethTisue commented #8036
  • 00:29
    SethTisue edited #8848
  • 00:28
    SethTisue edited #8848
  • 00:28
    SethTisue commented #8848
  • 00:28
    SethTisue commented #8848
  • 00:25
    SethTisue ready_for_review #8848
  • 00:25
    SethTisue edited #8848
  • 00:21
    scala-jenkins milestoned #8857
Fabio Labella
@SystemFw
my fiber talk expands on that point
Billzabob
@Billzabob
I’m curious if that race implementation is safe? I just discovered Future.firstCompleteOf and thought it would have a similar implementation but is uses an AtomicReference and a Promise under the hood.
OlegYch
@OlegYch
can you define 'safe'?
Billzabob
@Billzabob

Thread safe I guess. Or maybe there’s a problem with completing a Promise more than once? I thought it would just be:

def firstCompletedOf[A](futures: IterableOnce[Future[A]]): Future[A] = {
  val promise = Promise[A]()
  futures.iterator.foreach(promise.completeWith)
  promise.future
}

But I’m seeing this beast

Billzabob
@Billzabob
It looks like the AtomicReference is to guarantee that the Promise isn’t completed more than once. The above race will complete the Promise twice when each Future finishes and now I’m just wondering if that’s a problem since that race is from our codebase 😅
Billzabob
@Billzabob

Or I guess this would be better since you can specify the ExecutionContext:

def firstCompletedOf[A](futures: IterableOnce[Future[A]])(implicit executor: ExecutionContext): Future[A] = {
  val promise = Promise[A]()
  futures.iterator.foreach(_.onComplete(promise.tryComplete))
  promise.future
}

But ya still worried about the implications of completing a Promise twice.

Fabio Labella
@SystemFw
it will fail for the loser of the race
Billzabob
@Billzabob
Ya but what does fail mean here? tryComplete will return false and it will just be ignored and it wouldn't really matter right?
mmuhluri
@mmuhluri

Whenever I try to import package on sbt console I keep getting an error, What can be a problem?
scala> package com.packait.scala.book

<console>:1: error: illegal start of definition
package com.packait.scala.book
^

OlegYch
@OlegYch
you need 'import' instead of 'package'
Raitis Veinbahs
@siers
I was offered a scala coding position! :) I couldn't've done it without all the help in scala, fs2, circe, slick's and http4s's channels. :)
OlegYch
@OlegYch
congrats
Tobias Andersson
@biaandersson

I do this book, and according to the book this should work, but I get ArrayBuffer[B] IJ says it should be Seq[A] but it’s not inferred. (Its happens on the Sequence(…: _*) lines. My newbie head, can’t really understand why yet.

Anyone has an idea? Would be much appreciated.

case class Sequence[A](initialElems: A*) {

    private val elems = ArrayBuffer[A]()

    elems ++= initialElems

    def withFilter(p: A => Boolean): Sequence[A] = {
        val tmpArrayBuffer = elems.filter(p)
        Sequence(tmpArrayBuffer: _*)
    }

    def map[B](f: A => B): Sequence[B] = {
        val abMap = elems.map(f)
        new Sequence(abMap: _*)
    }

    def foreach(block: A => Unit): Unit = {
        elems.foreach(block)
    }
}
Louis Bettens
@lourkeur
@biaandersson I'm on my phone but I think it might be because ArrayBuffer is a scala.collection.mutable.Seq and that the varargs type might want an immutable one
Tobias Andersson
@biaandersson
Thanks, Louis, let me try.
Louis Bettens
@lourkeur
@biaandersson It changed in 2.13: now it requires immutable.Seq, it was laxer in 2.12
Tobias Andersson
@biaandersson
ah alright alright, thanks. I’ll keep that in mind and try to solve it.
Tobias Andersson
@biaandersson
@lourkeur, that worked like a charm. Thank you! :)
Seth Tisue
@SethTisue
@siers :tada:
Luis Miguel Mejía Suárez
@BalmungSan
@siers :smile:
Catalin Torge
@torgeadelin

Hi! I have a question about implicit generic types.
Is it possible to create a class, let’s say

class Button[T](var param1: T) {

}

such that when I initialise a new object new Button[Unit]() I don’t have to insert the Unit type and use it as an implicit(as in I’d like to just write new Button() and have it’s type Unit as default)?
I would still like to create other buttons like new Button[String]()

Rob Norris
@tpolecat
Yes, you can default to Unit.
@ class Button[T](var param1: T = ()) 
defined class Button

@ new Button() 
res1: Button[Unit] = ammonite.$sess.cmd0$Button@e280403

@ new Button("foo") 
res2: Button[String] = ammonite.$sess.cmd0$Button@626d2016
Catalin Torge
@torgeadelin
@tpolecat thanks!!!
Rob Norris
@tpolecat
:-)
Catalin Torge
@torgeadelin
@tpolecat , What about if I need to default to a different type? Is that possible?
Rob Norris
@tpolecat
Sure, val param1: T = <expr> and the default value will be <expr> and default T will be type of <expr>.
Luis Miguel Mejía Suárez
@BalmungSan
Or maybe create your own default typeclass? If you want multiple defaults for multiple types.
Catalin Torge
@torgeadelin

@tpolecat Ok, makes sense. But I’m encountering the following issue if I follow your idea.

class Btn[T](var param1: String, var param2: Int, var param3: T = ()) {
  param1 = param1.toUpperCase()

  def this(newParam1: String) {
    // this(param2)
    this(newParam1, 0) //polymorphic expression cannot be instantiated to expected type;
    //and if I try this(newParam1, 0, ()) -> gives type missmatch
  }
}

Any ideaS?

Rob Norris
@tpolecat
@torgeadelin yes, use a case class with overloaded apply. We really don't use secondary constructors much in Scala. I can't remember the last time I used one. See https://scalafiddle.io/sf/qaIEfRS/0
Catalin Torge
@torgeadelin
@tpolecat Oh, that’s awesome! Thanks a bunch for making the time to write the example. I really appreciate!
Rob Norris
@tpolecat
No worries, good luck!
Mario Galic
@mario-galic
Using VSC + Metals is it possible to go to sbt sources, for example, if I command+click on, say, scalaVersion in build.sbt, then it takes me to corresponding definition in Keys.scala?
mmuhluri
@mmuhluri
@OlegYch I did try to use import, I am still getting the same error.
OlegYch
@OlegYch
that would be weird
Seth Tisue
@SethTisue
@mario-galic (if nobody answers here, you might try the room scalameta/metals)
Hanns Holger Rutz
@Sciss
Is anyone aware of an sbt code-generator plugin that subsitutes @specialized, like rewriting trait Foo[$A] to trait FooI extends Foo[Int]; trait FooL extends Foo[Long] etc.?
sken
@sken77
is there a special reason why + doesnt work in typeclasses with syntax ops?
i have
trait Num[A] { def plus[B: Num](a: A,  b: B): A }
trait NumSyntax { implicit class NumSyntaxOps[A: Num](a: A) {
  def +[B: Num](b: B): A = Num[A].plus(a, b)
i get a value + is not a member of <A>
sken
@sken77
the other operators work fine
its just +
som-snytt
@som-snytt
@sken77
scala> new Object() + " is new"
       ^
       warning: method any2stringadd in object Predef is deprecated (since 2.13.0): Implicit injection of + is deprecated. Convert to String to call +
res0: String = java.lang.Object@4a7fd0c9 is new
Rich
@Rich2
Is it OK with 2.13.1 to have empty package objects, just for the package ScalaDoc comment? In the past I've found I've had to add a useless method to avoid problems.
Ender Tunc
@endertunc_gitlab
Hey all, do you we need a hashcode to equality check of two things in scala?
I mean we use == but how does it actually check that two things are equal?
Tobias Andersson
@biaandersson
Ender Tunc
@endertunc_gitlab
@biaandersson thanks. but again we call == on primitive types like int and string. Does it mean that at this point we trust scala to check equality correctly? How does scala do it under the hood?
Tobias Andersson
@biaandersson
It’s beyond my level of expertise so far, I am afraid Ender.