Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Adam Rosien
@arosien
it probably works most of the time, and doesn't work in weird ways
bblfish
@bblfish:matrix.org
[m]
There is a nice intro talk to it here: https://www.youtube.com/watch?v=8-b2AoctkiY I am just watching now.
Luis Miguel Mejía Suárez
@BalmungSan
AFAIK they always work, because they are exactly that just opaque type alias.
The downside is that they are very raw.
You need to re-create a lot of functionality.
Christopher Davenport
@ChristopherDavenport
They work, but the scopes and rules are pretty difficult to get rightl
Atleast if you want to combine static validation and opaque types, which I often do
Luis Miguel Mejía Suárez
@BalmungSan
Why do you need the inner scope?
bblfish
@bblfish:matrix.org
[m]
I was trying to use an opaque type a few days ago for a Long to refine it to an SfLong (which can have only 15 decimal digits), but I think I discovered that the compiler gave me a warning on pattern matching those, relating Scala loosing type information... (But I did not investigate much deeper and move to case classes..)
Christopher Davenport
@ChristopherDavenport
The inner scope is to allow the static validation to exist outside the scope of the opaque type, as inline and opaque types internal scope conflicts
That's my example of simplest I could manage that was equivalent to - https://github.com/ChristopherDavenport/nonemptystring/blob/master/modules/core/src/main/scala-2/io/chrisdavenport/nonemptystring/NonEmptyString.scala#L23 - Happy to simplify if folks have easier ways. I'm fairly certain I'll have to the same scoping, but now I can use literally rather than writing the custom macro validation myself.
Luis Miguel Mejía Suárez
@BalmungSan
@-@
bblfish
@bblfish:matrix.org
[m]
This is what I get
 Warning: /Volumes/Dev/Programming/Scala3/cosy/src/main/scala/run/cosy/http/headers/Signature-Input.scala:104:70 
[warn] 104 |    def created: Option[Long] = params.get(Token("created")).collect{case num: SfInt => num.long}
 |the type test for run.cosy.http.headers.Rfc8941.SfInt cannot be checked at runtime
So that is where opaque type SfInt = Long and with a simple extension method long to get out the Long again.
Adam Rosien
@arosien
in scala 2 you'd want to create an extractor (unapply), not sure what scala 3 requires
bblfish
@bblfish:matrix.org
[m]
Ah yes, of course. Let me see. Scala3 has some new extractor tech.
bblfish
@bblfish:matrix.org
[m]
I was able to get that pattern match to work by adding a TypeTest
given TypeTest[SfInt, Long] =
    new TypeTest[SfInt, Long]:
        def unapply(s: SfInt): Option[s.type & Long] = Some(s)
1 reply
It looks like Scala3 needs Optionless TypeTest for opaque types used in refinement contexts
Adam Rosien
@arosien
is there the equivalent of the scala 2 unapply where you can type the return value as Some to "guarantee" the extraction?
bblfish
@bblfish:matrix.org
[m]
Scala3 extractors are a superset of Scala2 ones.
Oleg Pyzhcov
@oleg-py
You can extract straight to tuple (including Tuple1) or e.g. AnyVal case class
zeroexcuses
@zeroexcuses
Let Vec2=(float, float), LineSegment = (Vec2, Vec2).
Is there a numerically stable way to figure out if two LineSegments intersect? (stable even in degenerate cases, i..e. two lines overlap, two linesparalle, one line is actually a point, etc ...)
Aly
@aly:scuttlebug.space
[m]

Is there a sort of scan-traverse? I.e. I have a

val start: Int
val str: String
val f: (Int, Char) => Option[Int]

and I want

val end: Option[Int]
Looking at Hoogle, this is foldM
How do I run it on a String? Efficiently at least
Oleg Pyzhcov
@oleg-py

you don't. Efficiently, at least.

You can get the Foldable[Iterable] instance from alleycats and do (str: Iterable[Char]).foldM(start)(f). I'm pretty sure that's gonna box every char tho

Christopher Davenport
@ChristopherDavenport
It really depends on the string. If it's a byte array backed string you are always going to see some performance loss at the char level.
If it's char array backed you can in a limited fashion rely on the approach above at limited cost.
Aly
@aly:scuttlebug.space
[m]
It's a string that I'm getting from fs2.text.utf8Decode
Oleg Pyzhcov
@oleg-py
aren't java.lang.String always char-array backed?
Aly
@aly:scuttlebug.space
[m]
So I could change the combinator that I'm using there if necessary
Christopher Davenport
@ChristopherDavenport
No, there are char array and byte array strings.
Technically different, but effectively the same.
Oleg Pyzhcov
@oleg-py
oh
depends on the jre I guess :D
Christopher Davenport
@ChristopherDavenport
I think you can just operate on the string. The performance hit as long as you are efficient in a single pass is normally not noticed
It's if you do backtracking it can hurt.
Cats-parse I highly recommend based on what you are presently describing.
Oleg Pyzhcov
@oleg-py
you'll have to be boxing characters to pass them to Function2 in the first place as well
Aly
@aly:scuttlebug.space
[m]
Ok, I'll just use a while loop with charAt then, and some vars
RE cats-parse: I'm writing a lexer generator that takes tmLanguage files and generates lexers because I don't want to have to maintain both a VSCode language file and a cats-parse grammar
Christopher Davenport
@ChristopherDavenport
You are doing parsing, so that library does it efficiently and is nice to work with.
Oleg Pyzhcov
@oleg-py
there goes the wisdom of benchmark first :D
Aly
@aly:scuttlebug.space
[m]

The while loop is easier to write :^)

(Eventually I'll see if the lexer is slow enough that I need to actually work on it)

Christopher Davenport
@ChristopherDavenport
Never start with that, imo. The boxing cost on single pass will probably not be noticeable. So start there.
Oleg Pyzhcov
@oleg-py
especially if your characters are mostly ASCII :D
Aly
@aly:scuttlebug.space
[m]
I already have code for the while loop :P
Hmm, Antimirov also does a while loop for this