Giter Site home page Giter Site logo

splitByType / splitByEnum about airstream HOT 6 OPEN

raquo avatar raquo commented on June 7, 2024 3
splitByType / splitByEnum

from airstream.

Comments (6)

HollandDM avatar HollandDM commented on June 7, 2024 2

I'm currently using something like this https://gist.github.com/HollandDM/446bb41a8c89607b140d3bf3297b2a92. This macro's main feature is to turn a bunch of code from this:

sealed trait Foo

final case class Bar(strOpt: Option[String]) extends Foo
enum Baz extends Foo {
  case Baz1, Baz2
}
case object Tar extends Foo

val splitter = fooSignal.splitMatch
  .handleCase { case Bar(Some(str)) => str } { (str, strSignal) => renderStrNode(str, strSignal) }
  .handleCase { case baz: Baz => baz } { (baz, bazSignal) => renderBazNode(baz, bazSignal) }
  .handleCase {
     case Tar => ()
     case _: Int => ()
  } { (_, _) => div("Taz") }
  .toSignal

Into this:

val splitter = fooSignal.
  .map { i =>
     i match {
        case Bar(Some(str)) => (0, str)
        case baz: Baz => (1, baz)
        case Tar => (2, ())
        case _: Int => (2, ())
     }
  }
  .splitOne(_._1) { ... }

(After macros expansion, compiler will warns this code "match may not be exhaustive" and "unreachable case" as expected).

As far as I know, "exhaustive matching" cannot be trigger by any mean beside defining a match case, as this is a compiler feature, not a scala's macros/runtime feature. So macros will need to do something similar to the code above. It's will be too much for us create exhaustive matching manually.

FYI: I think this is the entry for exhaustive match checking in scala 3 https://github.com/lampepfl/dotty/blob/main/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala#L825.

I can help with a PR if you want to go with this, but this is scala 3's macro only, and I don't have any experience with scala 2's macro, unfortunately.

from airstream.

felher avatar felher commented on June 7, 2024 2

I like this a lot. The implementation is quite complex compared to my initial one above, but the payoff is so much greater.

For example, this should work well with splitting a union of string literal types. Javascript has a ton of those. In fact, a common pattern for discriminated unions in typescript works exactly this way, as seen in the following link from the official typescript Documentation: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions

from airstream.

raquo avatar raquo commented on June 7, 2024 1

@HollandDM Thanks, this is awesome, it's pretty much what I was hoping for!

Don't worry about Scala 2, I wasn't planning to support it for the same reasons you mention. I'm ok with all macro-enabled features being Scala 3 only (not like there's a lot, it's just this).

I'll be happy with any PR / help, but I won't be able to include this in 17.x, it will go in a later version. I will only be able to give this an in-depth look in a few months, so just keep that timeline expectation in mind.

from airstream.

HollandDM avatar HollandDM commented on June 7, 2024 1

@raquo Don't worry, I can still use my internal implementation in the mean time. Take your time!

from airstream.

HollandDM avatar HollandDM commented on June 7, 2024

I do have some ideals about this, and would like to help.
SplitByType would be very welcomed, as my app usually required something similar to that.

from airstream.

raquo avatar raquo commented on June 7, 2024

@HollandDM I'll be happy to hear your ideas!

from airstream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.