Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 05:39
    SethTisue milestoned #802
  • 05:39
    SethTisue milestoned #470
  • 05:39
    SethTisue demilestoned #470
  • 05:38
    SethTisue assigned #802
  • 05:38
    SethTisue opened #802
  • 05:03
    SethTisue commented #1516
  • 05:02
    SethTisue commented #1516
  • 04:41
    SethTisue commented #1516
  • 04:40
    SethTisue converted_to_draft #1516
  • 04:40
    SethTisue commented #1516
  • 04:38
    SethTisue commented #1516
  • 04:35
    SethTisue commented #1516
  • 04:06
    SethTisue commented #1516
  • 03:33
    SethTisue commented #1516
  • 03:22
    SethTisue edited #1515
  • 03:21
    SethTisue edited #1516
  • 03:20
    SethTisue edited #1515
  • 03:19
    SethTisue labeled #1516
  • 03:19
    SethTisue assigned #1516
  • 03:19
    SethTisue opened #1516
saahm
@saahm
Nevermind
saahm
@saahm
      val fileStream = getClass.getResourceAsStream(path)
      val lines = Source.fromInputStream(fileStream).getLines()
      println(lines.length)
      lines.foreach(println)
So when I print the length of the iterator, it doesnt print the lines one line after it anymore, does using .length change something with that Iterator?
Because when I comment out the print(lines.length) it prints the lines.
Francois GORET
@fgoret

Hello, looks like the export clause does not exports the default values of a function's parameters. Using Scala 3.1.1-RC1 on
scalastie :

class A(s: String):
  def greeting(name: String = s) = s"Hello $name"

class B:
  val a = A("You")
  export a.*

val b = B()

println(b.a.greeting())      // works
println(b.greeting("John"))  // works
println(b.greeting())        // nope !

Is it a known restriction ?

Guillaume Martres
@smarter
No, I'd say that's a bug.
Francois GORET
@fgoret
@smarter Ok, I will open an issue on github lampfl/dotty.
Luis Miguel Mejía Suárez
@BalmungSan
@saahm iterators only have one use, if you call length on it, you just throw away all the data.
You can remeber that, or just do a toList after getLines so you can load all the data in memory an being able to use it multiple times.
Michael Thomas
@Michaelt293
I am writing a scala 2 macro at the moment and am trying to get the field types for case classes using WeakTypeTag. Using typeSignature, I get the following for Int field - => Int. I want to inspect using something like in =:= typeOf[Int]. I'm a bit confused on what i should be doing?
Michael Thomas
@Michaelt293
Actually, if I use returnType, I seem to get what I need.
Luis Miguel Mejía Suárez
@BalmungSan
Uhm, there was a question here a moment ago.
Two advises:
  1. Use proper types, rather than a List[List[Any]] where the inner list always has two elements of different types; use a List[(Int, A)]
  2. The Scaladoc is your friend: https://www.scala-lang.org/api/current/index.html
Hope you still manage to see this :)
Sakitha
@Sakitha
yeah, I'm here .. thought it was a dumb question to ask, is it bad to have a for loop that wouldn't yield a value but instead change a predefined array ?
Luis Miguel Mejía Suárez
@BalmungSan

@Sakitha

thought it was a dumb question to ask

There are no dumb questions, there are fools that don't ask.

Is it bad to have a for loop that wouldn't yield a value but instead change a predefined array?

Well, it depends on a lot of things, specially what is your definition of "bad".

What I can say is that, in general, idiomatic Scala prefers immutable solutions over mutable ones.
There are 2 reasons for that:
  1. Shared mutability can become problematic when you have concurrency.
  2. Immutable solutions powered by the stdlib rich operations are usually more maintainable than a mutable loop (although, this is somewhat subjective)
As such, if your mutation is contained in the scope of a single method then it is fine.
And if you use that mutation to either improve performance or make the code easier to maintain to your team then that is great.
However, what I personally think is that it is easier to be 100% consistent than 99%
Thus, if all the code base is immutable I would rather make each method immutable even if their contained mutability is fine just for the sake of consistency.
Sakitha
@Sakitha
I just want to learn the functional way of coding, Thank you for the information and for the example
Luis Miguel Mejía Suárez
@BalmungSan
And I would only break that rule if I have enough evidence that the immutable version is hurtful to the performance of the app and the mutable one fixes that.
Which, IME, has never been the case; but your mileage may change.

I just want to learn the functional way of coding, Thank you for the information and for the example

Sure!
No worries and welcome to the community!

Feel free to keep asking questions as you continue to practice.
The only way for this to become easy is to code, and code, and code some more.
Sakitha
@Sakitha
ok, thanks
Luis Miguel Mejía Suárez
@BalmungSan
Finally, this chat is slowly dying.
If you can and want; I would recommend joining the discord server instead: https://discord.gg/7aUsmzyD
Sakitha
@Sakitha
will do.. really appreciate your time and efforts
Luis Miguel Mejía Suárez
@BalmungSan
You are welcome :)
Oliver Winks
@PaperPlaneSoftware
trait A { def hello: String }
trait B { def hello: String }

def test(x: A | B) = x.hello // error: value `hello` is not a member of A | B
surely this is incorrect
N.S. Cutler
@godenji
@smarter is it possible to combine Match Types with generic type argument(s) such that the application of the type match can occur solely based on type? (i.e. with no value present).
type Id = Int | Long | UUID

trait MappedTo[+T <: Id] extends Any:
  def value: T

type GetType[T <: MappedTo[_]] = T match
  case MappedTo[Int] => Int
  case MappedTo[Long] => Long
  case MappedTo[UUID] => UUID

class Extract[T <: MappedTo[_]](val map: T => GetType[T])

given extract[T <: MappedTo[_]]: Extract[T] = new Extract[T](_.value) // ERROR
Found: _$1.T
| Required: GetType[T²]
|
| where: T is a type in trait MappedTo with bounds <: Id
| T² is a type in given instance extract with bounds <: MappedTo[?]
|
|
| Note: a match type could not be fully reduced:
|
| trying to reduce GetType[T]
| failed since selector T
| does not match case MappedTo[Int] => Int
| and cannot be shown to be disjoint from it either.
| Therefore, reduction cannot advance to the remaining cases
|
| case MappedTo[Long] => Long
| case MappedTo[java.util.UUID] => java.util.UUID

So, in val map: T => GetType[T], T is unknown and therefore the match type fails.

I guess I'm looking for a lazy Match Type if that makes sense, or is even possible.

Use case is attempting to use Match Types as a means to replacing removed Abstract Type Projections in Scala 3.
Eric K Richardson
@ekrich
@godenji If it is not to inconvenient, most people have moved to Discord - https://discord.gg/scala
N.S. Cutler
@godenji
Most people, except for Guillaume :), thus the question placed here.
Eric K Richardson
@ekrich
Oh, I think contributors is better.
rnd4222
@rnd4222_gitlab
Is there something like shapeless Generic/LabelledGeneric but for trait methods / function arguments?
Artem Egorkine
@arteme
in scala 2.13 is it okay to generalize as def process[A, C[x] <: IterableOps[x, C, C[x]](coll: C[A]): C[A] when I want to map over a collection C[A] to get back a C[A]? Or is there a better way?
Seth Tisue
@SethTisue
4 replies
Todd O'Bryan
@toddobryan

I just found some interesting behavior in Scala 3.

infix case class ~[T1, T2](_1: T1, _2: T2)

def main(): Unit =
   val ab: Int ~ Int = ~(1, 3)
  println(ab)

results in value unary_~ is not a member of (Int, Int). In other words, ~(1, 3) is getting parsed as unary_~ plus Tuple2(1, 3) instead of function name ~ + arguments (1, 3). Is this a change in how Scala 3 parses, a known bug, or have I discovered something interesting?

~.apply(1, 3) seems to work as a workaround, but is a little annoying.
Todd O'Bryan
@toddobryan
I also tried to create an infix version using an extension method:
infix case class ~[T1, T2](_1: T1, _2: T2)

extension [T1](t1: T1)
  infix def ~[T2](t2: T2) = ~.apply(t1, t2)

@main
def main(): Unit =
  val ab: Int ~ Int = 1 ~ 3
  println(ab)
This resulted in ~ is already defined as object ~ in /path/to/the/scala/file
Ghost
@ghost~61ab4f6e6da03739848bd7e2
@toddobryan as per Eric's comment, most people have moved over to discord https://discord.gg/scala
Todd O'Bryan
@toddobryan
Oh. Thanks.
Thorarinn Sigurdsson
@thsig

Hello there! I just started poking around with Scala and sbt, but am running into some gnarly errors right out of the box that I'm finding hard to diagnose.

First, I installed sbt via Homebrew (I'm on OSX) and followed the instructions in this example project: https://github.com/tototoshi/sbt-hot-reload-example

At first, it worked like a charm. Then I came back to it a few minutes later, did another cd framework && sbt publishLocal && cd ../app && sbt run, and got the following error:

INFO: No global web.xml found
Dec 06, 2021 7:08:26 PM org.apache.tomcat.util.scan.StandardJarScanner processURLs
WARNING: Failed to scan [file:/Users/ths/.sbt/boot/scala-2.12.14/org.scala-sbt/sbt/1.5.5/scala-reflect.jar] from classloader hierarchy
java.io.IOException: java.lang.reflect.InvocationTargetException
    at org.apache.tomcat.util.compat.Jre9Compat.jarFileNewInstance(Jre9Compat.java:209)
    at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:65)
    at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49)
    at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:383)
    at org.apache.tomcat.util.scan.StandardJarScanner.processURLs(StandardJarScanner.java:318)
    at org.apache.tomcat.util.scan.StandardJarScanner.doScanClassPath(StandardJarScanner.java

Like it's looking for a JAR but not finding it.

I tried uninstalling and reinstalling sbt, installing it via sdk instead, but to no avail.
Seth Tisue
@SethTisue
@thsig do I remember right that you already got help with this on Discord?
Eric K Richardson
@ekrich
@SethTisue Yes, he got some help over there and was welcomed and is all set for now.