Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 21:10
    Sciss opened #2031
  • 21:08
    joroKr21 commented #2027
  • 20:54
    superseeker13 commented #9637
  • 20:30
    dwijnand synchronize #1828
  • 18:39
    b-studios commented #1246
  • 18:37
    b-studios commented #1910
  • 18:37
    b-studios commented #1925
  • 18:36

    b-studios on main

    Settle on variable name fnf for… Use f-strings in Python to matc… Cleanup whitespace and 2 more (compare)

  • 18:36
    b-studios closed #2024
  • 18:20
    SethTisue synchronize #1416
  • 18:00
    SethTisue commented #1828
  • 18:00
    SethTisue commented #1910
  • 18:00
    SethTisue review_requested #1925
  • 17:59

    SethTisue on main

    Added to the tuple intro, relat… Update _overviews/scala-book/pr… Update _overviews/scala-book/pr… and 1 more (compare)

  • 17:59
    SethTisue closed #1932
  • 17:59
    SethTisue commented #1952
  • 17:59

    SethTisue on main

    Add consistency to parameter li… Add alternative formatting Re-word and 1 more (compare)

  • 17:59
    SethTisue closed #1952
  • 17:59

    SethTisue on main

    a weird sentence changed Merge pull request #1978 from x… (compare)

  • 17:59
    SethTisue closed #1978
Rob Norris
@tpolecat
This hasn't been merged into Lightbend Scala but it might if someone were to pick it back and up and shepherd it through.
I don't know what dotty does in these cases.
Joe Pallas
@jpallas
So long as people understand that exhaustiveness checking for unsealed types doesn't perform magic, it just insists that there be a default case.
vijendra singh
@viju0731_twitter
How do get hash string from string using SHA512
vijendra singh
@viju0731_twitter
Thanks i created the sha512 function in scala
vijendra singh
@viju0731_twitter
in digesUtil all hash libs are available
Dominic Egger
@GrafBlutwurst
apache commons codec? yeah thats a wrapper around javas MessageDigest it makes it a bit nicer to work with
phip123
@phip123
Can anyone explain to me, when I should use type From = String or a value class like class From(val from: String) extends AnyVal?
Dominic Egger
@GrafBlutwurst

so the latter can be very useful to avoid some annoying bugs. e.g.

val userAccountMap: Map[Int,Int]

final case class UserID(id:Int) extends AnyVal
final case class AccountID(id:Int) extends AnyVal

val userAccountMap2: Map[UserID, AccountId]

makes sure you don't mix up the two

the former type alias can be sometimes useful just for readability From might be more telling than String but it's easy to overdo and sometimes a bit of a pain when debugging. types can shine when you need to do e.g. partial application type StringKeyMap[A] = Map[String, A] for some T[_] constraint.
Dominic Egger
@GrafBlutwurst
there's more you can do with both but just for example purposes
phip123
@phip123

Thanks! If it's okay for you, I would like to have your opinion on my approach to model an Email. :)

  final case class Domain(domain: String) extends AnyVal
  final case class Name(name: String) extends AnyVal
  final case class User(name: Name, domain: Domain)
  final case class Data(data: String) extends AnyVal
  final case class Subject(subject: String) extends AnyVal
  type Users = List[User]

  final case class Email(from: User = User(Name(""),Domain("")),
                         to: Users = List(),
                         data: Data = Data(""),
                         subject: Subject = Subject(""))

Is type Users = List[User] reasonable/justified or unnecessary?

Fabio Labella
@SystemFw
@phip123 I would avoid it
type synonyms in general are in my opinion a false help: they look useful, they end up being harmful
either use a class (if you have some invariants to enforce), or just the plain List
phip123
@phip123
ok, thanks!
Shivam Panjeta
@shivampanjeta
Regex query
12,34,56,78 to 123456.78
12.34.56,78 to 123456.78
12,34,56.78 to 123456.78
basically I want to handle different currencies to double, without knowing the user's locale
sheryl11
@sheryl11
image.png
image.png
Shivam Panjeta
@shivampanjeta
p.replaceAll("[,.](?=[0-9]+[,.])", "").replaceAll(",", ".");
does this thing look fine to you guys for my use case?
Luciano
@lJoublanc
@shivam-panjeta have you looked at whether Pattern has conversions that can do this for you automatically?
I recall it had some special handling of locales.
Shivam Panjeta
@shivampanjeta
But I don't know the locale
Luciano
@lJoublanc
But the system does..
Shivam Panjeta
@shivampanjeta
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234");
double d = number.doubleValue();
say this
but I don't want to take locale into account
Sherif Mohamed
@sherifkandeel

Could someone explain to me how flatmap in Try works? in case of success, this is how you apply flatmap

override def flatMap[U](f: T => Try[U]): Try[U] =
    try f(value) catch { case NonFatal(e) => Failure(e) }

But it doesn't make sense to me, because f(value) returns Try[U] which will never throw exception, why there's catch?

Fabio Labella
@SystemFw
@sherifkandeel it also guards against the case where f throws an exception before returning Try
Sherif Mohamed
@sherifkandeel
@SystemFw that is what confuses me. f is supposed to encode the exceptions in the Try that's the whole point of returning a Try so I was skeptical it's just an extra safeguard
Abdhesh Kumar
@abdheshkumar
@sherifkandeel Try.flatMap is a abstract method and it is overridden by sub-types(Success,Failure). let say you have val t: Try[String] = Success("Hello") so when you call it t.flatMap then It will call Success.flatMap. Success.flatMap know to how to run given function which is happy path.
Fabio Labella
@SystemFw
@sherifkandeel yes, it's supposed to do that but the code is guarding against the case where this doesn't happen
Sherif Mohamed
@sherifkandeel
@abdheshkumar True, but the override implementation I copied was from Success because the case of Failure is pretty simple.
@SystemFw Makes sense. Thank you :)
Fabio Labella
@SystemFw
well, actually I need to take a look at the real code
nah, that's it I think
Luciano
@lJoublanc
@shivam-panjeta that's what I was going to suggest. you can use Locale.getDefault and it should pick up the system locale I think. See the Locale javadoc.
Abdhesh Kumar
@abdheshkumar
Every function given to try is also handled the error.
Fabio Labella
@SystemFw
iirc map for Try does the same
Sherif Mohamed
@sherifkandeel
@SystemFw No, not really, it's just:
  override def map[U](f: T => U): Try[U] = Try[U](f(value))
Abdhesh Kumar
@abdheshkumar
@sherifkandeel Try.apply handle an exception`
Fabio Labella
@SystemFw
scala> scala.util.Try(1).map(x => (throw new Exception): Int)
res1: scala.util.Try[Int] = Failure(java.lang.Exception)
Sherif Mohamed
@sherifkandeel
@abdheshkumar @SystemFw ah, yes. so you don't need to handle again, since you're already wrapping it in try
The Try in Try[U](f(value)) takes care of that
you could've done the same for flatmap, but then you would end up with Try[Try[U]]
Luciano
@lJoublanc

Guys I have a question about implicit resolution. The resolution rules look by default in the companion of any types that are in the signature. So e.g. for (implicit fa: F[A]) it will check both the companion of F[_] and A and their related classes etc.
However, if I'm unable to extend these (e.g. shapeless.Typeable == F[_] and fs2.Chunk == A, both from external libraries), then I have to resort to explicitly importing the instances. Is there any way around this, to avoid the explicit import?
For a concrete example:

trait MyTypeclass[...] {
}

object MyTypeclass {
  implicit def typeableInstance[A]: Typeable[Chunk[A]] = ???
  implicit def myTcInstance(implicit: Typeable[Chunk[_]]): MyTypeclass = ???
}

When I want to use my typeclass in the above example, I always need to do

import MyTypeclass._
implicitly[MyTypeclass]

And if the library user doesn't know they need the first import statement. Really annoying. How can I work around this?

Fabio Labella
@SystemFw
right, you have an orphan instance, which generally requires an import