Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Ghost
    @ghost~540393fe163965c9bc2018ce
    that's really annoying, because lambdas are one of the most compelling usecases for seeing the text
    btw, I have a PR open in this area...
    Ólafur Páll Geirsson
    @olafurpg
    It's not an inherent limitation, it's an implementation quirk
    Ghost
    @ghost~540393fe163965c9bc2018ce
    lihaoyi/sourcecode#42
    btw builds are all failing until scala-native is updated
    (I really wish native/scalajs building happened in a parallel universe)
    nafg
    @nafg
    @olafurpg interesting. Does that at least get past the type inference issue? In other words would this work? def m(f: String => Boolean); DebugFun(_ == "hello")
    Ólafur Páll Geirsson
    @olafurpg
    @nafg I didn't get past the "Identifier expected but integer literal found." error
    but it seems to infer the correct type
    nafg
    @nafg
    it's really an orthogonal matter to sourcecode
    Oron Port
    @soronpo
    Is there a way I can get the name in the following case:
    abstract class Foo {//Can add any implicit I want here
      def getName =  ???
    }
    val nice = new Foo {}
    assert(nice.getName == "nice")
    nafg
    @nafg
    Doesn't Name work?
    Oron Port
    @soronpo
    Nope
      abstract class Foo(implicit n : sourcecode.Name) {
        def getName = n.value
      }
      val nice = new Foo {}
      println(nice.getName)
    Prints $anon
    Oron Port
    @soronpo
    The following works as expected:
      abstract class Foo(implicit n : sourcecode.Name) {
        def getName = n.value
      }
      object Foo {
        def apply()(implicit n : sourcecode.Name) = new Foo {}
      }
      val nice = Foo()
    Prints nice
    For my use-case I must have the new Foo{} form. Java reflection it is :worried:
    Oron Port
    @soronpo
    Hmmm... sourcecode.Enclosing knows nice. Perhaps sourcecode.Name should special case anonymous classes. @lihaoyi your thoughts?
    Li Haoyi
    @lihaoyi
    yeah
    it’s probably a bug
    iirc i wanted to skip them
    skip anonymous enclosing owners to find the first named one
    send a pr
    Oron Port
    @soronpo
    created a PR. lihaoyi/sourcecode#48
    Oron Port
    @soronpo
    @lihaoyi What are your thoughts about expanding the library so it would return a singleton string instead of string? With singletons we can create compile-time computations and checks?
    Li Haoyi
    @lihaoyi
    yo
    uhh no idea
    don’t know anything about singleton types :
    :p
    maybe send a pr showing what it would look like and what use cases it would allow?
    Oron Port
    @soronpo
    It can be used for linting like this (along with singleton-ops)
    class Foo
    def foo[N](implicit n : sourcecode.Name.Aux[N], r : Require[SubString[N, 3]=="foo", "variable name must begin with 'foo'"]) : Foo = new Foo
    
    val foo1 = foo() //OK
    val goo1 = foo() //Compile-time fail: variable name must begin with 'foo'
    Additionally, I can create custom naming compositions to occur at compile-time, instead of run-time.
    Nick Childers
    @Voltir
    er, has anyone seen this error before: could not find implicit value for parameter name: sourcecode.Name
    erp, nevermind
    was 2.12.5 macro death - upgrading to 2.12.6 fixed it
    Oron Port
    @soronpo

    @lihaoyi, consider the following:

    abstract class Foo(implicit n : sourcecode.Name) {
      def getName = n.value
    }
    val wrapper : Foo = {
      println(new Foo{}.getName)
      new Foo{}
    }
    println(wrapper.getName)

    This will print

    wrapper
    wrapper

    Is it possible to modify the macro so it would print:

    $anon
    wrapper

    ?

    Ólafur Páll Geirsson
    @olafurpg
    @soronpo I think the owner chain is identical for those two
    Oron Port
    @soronpo
    It is, as far as I can tell. Is there no way to know which is the last expression (in the block)?
    Matthew Tovbin
    @tovbinm
    Howdy, I am trying to get a handle on the class name for lambda functions like this Seq(1, 2, 3).map(x => x + 1).map(x => x / 2). In Scala 2.11.x it becomes class anonfun$1 which is super ugly.
    I would like to have the generated class name to be a bit nicer. Say MyAppFunL1C20 meaning file MyApp.scala + Line 2 + Column 20.
    I believe it's not currently possible to do with sourcecode library, so my question is - is it valuable enough to have in a library? or should I try writing my own macro for it?
    Oron Port
    @soronpo
    I think this is very application specific
    You can have an implicit that gets both name and location and in your application you implement the mechanism if (x.name.value.contains("$")) ...
    Matthew Tovbin
    @tovbinm
    I would like to be able to serialize a sequence of lambda transformations and it seems to be possible now with scala 2.11.x just by just serializing the class names anonfun$1, anonfun$23, anonfun$41 etc, and then using reflection to insatiate classes.
    the problem that these classes are fragile to code changes and do not uniquely identify the actual function in the user's code.
    This message was deleted
    Wojtek Pituła
    @Krever
    Hey all, I would like to extract a method body (source code) or body of a code block (both are fine). I think its not possible with current impl (Text captures last expression in the block). It feel like something relatively easy but I cant really figure out how to approach this. I looked ad Text macro implementation but its not helpful in this case.
    The problematic part is: how to get code for given Tree/Expr.
    Li Haoyi
    @lihaoyi
    @Krever I don't think it's easy to do with the current macro API. Text is meant to do it, but it doesn't (and has bugs apart from that)
    might need to do something aggressive, like re-parse the whole file in the macro and find the piece of text you care about
    Wojtek Pituła
    @Krever
    I have very little experience with macros but I was thinking about having something like def text[T](thunk: =>T) = macro xxxand then rely on rangepos to get relevant part of the file. But this a long shot as I have never done it before and rely only on stuff I heard , so I cant say if it can work
    Li Haoyi
    @lihaoyi
    possible. RangePos didnt work well back in 2.11 and 2.12, but maybe now in 2.13 they’ve fixed all the old issues