Giter VIP home page Giter VIP logo

Comments (4)

bishabosha avatar bishabosha commented on September 25, 2024

This looks like its because of the desugaring - i.e

(foo =  _.length) : (foo: String => Int)

becomes

NamedTuple.withNames(Tuple1(_.length))[Tuple1["foo"]] : (foo: String => Int)
//                          ^^^^^^^^
//                          nested lambda with no expected type

it might be worth reconsidering this desugaring, or perhaps tunnelling through any expected type to the argument of withNames.

A tentative plan - perhaps we can add a type ascription to some "watcher" type, that then is resolved on the outside -

(NamedTuple.withNames(Tuple1(_.length): <watch[target]>)[Tuple1["foo"]] : <target>) : (foo: String => Int)

from scala3.

odersky avatar odersky commented on September 25, 2024

Named tuples are constructed with NamedTuple.withNames:

  extension [V <: Tuple](x: V)
    inline def withNames[N <: Tuple]: NamedTuple[N, V] = x

So the call looks like

NamedTuple.withNames[V](tuple)[N]

It's important to have two type parameter lists because the V is usually inferred but the N needs to be given explicitly. But they don't nesessarily need to come in that order. One could also imagine to build named tuples using a function like this

def build[N <: Tuple]()[V <: Tuple](x: V): NamedTuple[N, V]

Maybe that would fix the problem. I think it might be worth trying out, but I don't have the bandwidth to do it myself right now.

from scala3.

bishabosha avatar bishabosha commented on September 25, 2024

This solution seems to work without needing clause interleaving, and leaves no trace after inlining:

object NamedTupleBuilder {
  opaque type Builder[N <: Tuple] = Unit
  def apply[N <: Tuple]: Builder[N] = ()
}
extension [N <: Tuple](inline b: NamedTupleBuilder.Builder[N])
  transparent inline def build[V <: Tuple](inline v: V): NamedTuple[N, V] = v.withNames[N]

then we can have the example here:

val m: (foo: String => Int, bar: String => Int) =
  (foo = _.length, bar = _.length)

desugar to

val m: (foo: String => Int, bar: String => Int) =
  NamedTupleBuilder.apply[("foo", "bar")].build((_.length, _.length))

and it infers perfectly.

here is the tree after typer:

scala> val m: (foo: String => Int, bar: String => Int) = NamedTupleBuilder.apply[("foo", "bar")].build((_.length, _.length))
[[syntax trees at end of                     typer]] // rs$line$5
package <empty> {
  final lazy module val rs$line$5: rs$line$5 = new rs$line$5()
  final module class rs$line$5() extends Object() { this: rs$line$5.type =>
    val m: (foo : String => Int, bar : String => Int) =
      NamedTuple.withNames[(String => Int, String => Int)](
        Tuple2.apply[String => Int, String => Int](
          {
            def $anonfun(_$1: String): Int = _$1.length()
            closure($anonfun)
          },
          {
            def $anonfun(_$2: String): Int = _$2.length()
            closure($anonfun)
          }
        )
      )[(("foo" : String), ("bar" : String))]
  }
}

val m: (foo : String => Int, bar : String => Int) = (rs$line$5$$$Lambda$2022/0x000000012d6005d8@5e08ed28,rs$line$5$$$Lambda$2023/0x000000012d6009a8@24b05292)

from scala3.

bishabosha avatar bishabosha commented on September 25, 2024

I have implemented a fix in #20497 - which also includes a variant of https://github.com/bishabosha/ops-mirror that makes use of named tuples to implement server logic

from scala3.

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.