Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 03:19

    SethTisue on 2.12.x

    add akka-http-webgoat (#1017) (compare)

  • 03:19
    SethTisue closed #1017
  • 03:08
    SethTisue labeled #8584
  • 02:01
    som-snytt commented #8586
  • 02:00
    som-snytt commented #8586
  • 01:54
    som-snytt commented #660
  • 01:18
    som-snytt commented #8586
  • 01:12
    joroKr21 commented #8586
  • 01:09
    joroKr21 commented #8586
  • 01:08
    joroKr21 commented #8586
  • 01:08
    joroKr21 commented #8586
  • 01:05
    som-snytt commented #8586
  • 01:00
    joroKr21 commented #660
  • 00:54
    joroKr21 commented #8586
  • 00:30
    som-snytt commented #8586
  • 00:13
    diesalbla labeled #8583
  • Dec 10 23:49
    SethTisue commented #8586
  • Dec 10 23:42
    SethTisue commented #1017
  • Dec 10 23:40
    SethTisue ready_for_review #1017
  • Dec 10 23:40
    SethTisue synchronize #1017
Habris Mun
@HabrisMun_twitter
hi guys
if i have an array Array((1,2)) why can't i use Array(1,2))(0)
it says smoething like, scala.reflect.ClassTag
Habris Mun
@HabrisMun_twitter

also, let's say i want to find out if a string appears in my Array[(Int, String)].

Let's say the string is 'hello.com'. My array looks like such: Array((5, "hello.com")). Is there any way to get this to work?
Array((5, "hello.com")) contains (_, "hello.com")

since i don't get about the int value ?
Luis Miguel Mejía Suárez
@BalmungSan

@HabrisMun_twitter sadly you have encountered some of the little corner cases that are unpleasant about Scala.

Let's check at the documentation(implicitevidence$5:scala.reflect.ClassTag[T]):Array[T]) it says something like:

def apply[T](xs: T*)(implicit arg0: ClassTag[T]): Array[T]

So, it seems that it takes a second parameter group with an implicit argument about some ClassTag thing... that is due the way arrays work on the JVM level, is an implementation detail that you shouldn't bother too much (at least for now).

So when you do:

List(1, 2, 3)(0)

It is actually doing:

List(1, 2, 3).apply(0)

However, On your case, the compiler believes that you really want to pass the implicit parameter explicitly, instead of calling the apply.
You can solve it by splitting the call to a second line or calling apply directly, like:

val arr = Array((1, 2))
val first = arr(0)

// or
Array((1, 2)).apply(0)

On both cases what happens is that the compiler fills the implicit parameter for you (that is the point of it being implicit) and it is clear that you really want to call the apply.

Hope that explains the problem.

BTW, Just for the record, this is one of the things that Scala 3 will solve (which is great!!) because then you will need to be explicit about passing an implicit argument explicitly, so this will no longer be ambiguous.

Otto Chrons
@ochrons
you can use for example Array((5, "string")).exists(_._2 == "string")
Luis Miguel Mejía Suárez
@BalmungSan
For your second question:
arr.exists{ case (_, str) => str == "hello.com" }
Damn, I am the only one that needs to reload the page to see new messages?
I feel like a caveman hitting f5 every minute.
Habris Mun
@HabrisMun_twitter
my page formatting is weird
idk whats wrong with it
but thx for your answers guys!
im glad its just a subtle detail i dont have to worry about i thought i was an idiot
Otto Chrons
@ochrons
even experienced Scala devs stumble on that explicit implicit parameter from time to time :wink:
Luis Miguel Mejía Suárez
@BalmungSan
^^ yeah is one those things that is just unpleasant of the language. Really looking forward for the fix in Scala3
Rob Norris
@tpolecat
Also don't use Array. Use List or Vector or something.
Array is super-specialized for low-level JVM stuff and you rarely need it in normal code.
Saif Addin
@saif-ellafi
Hi, how can I force users of an API to create a new instance of a class only through a specific method? basically I want to prohibit users to create instances directly.
If I set the class to private however, they won't be able to use it's functions normally though
or have other functions require such private class
I'd like to enforce some requirements before a class is instantiated
Luis Miguel Mejía Suárez
@BalmungSan

@saif-ellafi you want a private constructor + a factory.

final class Foo private (...) {
}

object Foo {
  def apply(...): Foo = {
    // preconditions.
    new Foo(...)
  }
}

You may even return an Option[Foo] instead to handle unmatched pre-conditions.

@saif-ellafi you want a private constructor + a factory.

final class Foo private (...) {
}

object Foo {
  def apply(...): Foo = {
    // preconditions.
    new Foo(...)
  }
}

You may even return an Option[Foo] instead to handle unmatched pre-conditions.

Rob Norris
@tpolecat
Or a sealed trait (or sealed abstract class) and a factory method that returns anonymous instances. That’s a fairly common pattern.
Luis Miguel Mejía Suárez
@BalmungSan
Oh right, that is good one ^^
Lambda Developer
@chetkhatri

I have a 25 Spark jobs in Scala, each job requires configuration property values. At the moment, every class reads configLoader and does read that and each job has duplicate variables / functions. I am thinking to create a common class and pass args to constructor to generate property variables. and in all the spark jobs create an object and access each variables. another thought, I have is instead of class create a scala object and import that object._ , so I don't have to create mapping such as,

val commonUtil = new ConfigLoader
val database_password = commonUtil.database_password
val database_port = commonUtil.database_port

etc. What is the best practice to do?

Christopher Davenport
@ChristopherDavenport
Working on some java interop, is this cleanest way to go from enumeration to list?
def enumerationToList[A](e: java.util.Enumeration[A]): List[A] = {
      val builder = new mutable.ListBuffer[A]
      while (e.hasMoreElements()){
        val next = e.nextElement()
        builder.+=(next)
      }
      builder.result
    }
Luis Miguel Mejía Suárez
@BalmungSan

@ChristopherDavenport if you can use Java 9 you may use this:

def enumerationToList[A](e: java.util.Enumeration[A]): List[A] = {
  val builder = new mutable.ListBuffer[A]
  e.asIterator.forEachRemaining(builder.addOne) // Disclaimer, I do not have Java 9 so I couldn't test that it compiles.
  builder.result()
}

Not sure if it looks cleaner for you.
Apart from that, I do not see any other way.

Rob Norris
@tpolecat
Enumeration is old school.
Christopher Davenport
@ChristopherDavenport
I'm writing library code, so I'll have to stick with java 8 capabilities till its actually dead. Trying to expose the keys of a ConcurrentHashMap in a reasonable scala fashion. So I guess ConcurrentHashMap is old school. :smile:
Luis Miguel Mejía Suárez
@BalmungSan

On Java 8 you may:

java.util.Collections.list(e).iterator.forEachRemaining(builder.addOne)

But for me, it seems like a lot of conversions in just one line.

I would just leave the while and never open the file again lol
Oh wait final result is a Scala List.
java.util.Collections.list(e).asScala
even better e.asScala.toList
Christopher Davenport
@ChristopherDavenport
I'm considering it, but that means I'd have to interface with the collections api across 2.12 , 2.13
Luis Miguel Mejía Suárez
@BalmungSan

https://github.com/scala/scala-collection-compat

Has worked like a charm for me.

But, I am not sure if it is wrong to use it as a dependency on a library?
Christopher Davenport
@ChristopherDavenport
Its not wrong, I've ended it up on projects that were using the old collections, but I feel a little guilty adding a dep if I don't need to.
The one thing One of the things I've learned writing libraries is how dependency averse some codebases become over time.
Luis Miguel Mejía Suárez
@BalmungSan
Well AFAIK the compat library is very small for example on 2.13 it is basically empty and on 2.12 it is just a couple of wrappers and implicits.
I wouldn't be too much different if you end writing the wrappers by yourself.
Luis Miguel Mejía Suárez
@BalmungSan
BTW, just out of curiosity, why a List @ChristopherDavenport ?
You said they were the keys of a concurrent hash map, wouldn't a Set be a better option?
Christopher Davenport
@ChristopherDavenport
In many ways yes. In many ways no. Set is a pretty terrible functional data structure, lacking even a true functor instance. So I went with what I though was a safe compromise. Perhaps I will reconsider at some point.
Most of the reason I'd want the keyset is in order to traverse and run some operation across the keys.
Luis Miguel Mejía Suárez
@BalmungSan
Uhm, So given a Set doesn't have a lawful Functor it wouldn't have a lawful Traverse right?
Christopher Davenport
@ChristopherDavenport
Yep, Traverse is a traversable functor, so anything with Traverse must be able to implement Functor
Luis Miguel Mejía Suárez
@BalmungSan
I have read the explanation of why a Set is not a lawful Funtor from some gist of Rob.
But then I wonder if one wants to represent the constraint of uniqueness then one is doomed to treat that effect completely different than the others?