FAQ
TL;DR: will Go ever have support for "code as data" such as C# has with
Expression Trees?

C# has this really nice feature called Expression Trees. Basically you can
write code and it becomes available as data (in the form of an expression
tree). Here is an example of expression trees using lambda:

class Foo {
   public string Name;
   public int Age;
}

Expression<Func<Foo, bool>> exp = foo => foo.Name != null && foo.Age > 21;

(for those unfamiliar with C# lambda, the above syntax creates a function
which receives foo of type Foo and evaluates into a bool).

C# compiler will compile the code above doing all type checking but instead
of generating actual binary, it will translate that expression into an
object that represents the code. One can later navigate through that code
translating it into, say, SQL:

... WHERE Name IS NOT NULL AND Age > 21

AFAIK, no other language has such feature. I wonder whether Go has any
plans into that direction.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • David Crawshaw at May 11, 2014 at 2:27 pm
    Something very similar could be built as a library using the reflect
    package.

    On Sat, May 10, 2014 at 10:23 PM, Igor Gatis wrote:

    TL;DR: will Go ever have support for "code as data" such as C# has with
    Expression Trees?

    C# has this really nice feature called Expression Trees. Basically you can
    write code and it becomes available as data (in the form of an expression
    tree). Here is an example of expression trees using lambda:

    class Foo {
    public string Name;
    public int Age;
    }

    Expression<Func<Foo, bool>> exp = foo => foo.Name != null && foo.Age > 21;

    (for those unfamiliar with C# lambda, the above syntax creates a function
    which receives foo of type Foo and evaluates into a bool).

    C# compiler will compile the code above doing all type checking but
    instead of generating actual binary, it will translate that expression into
    an object that represents the code. One can later navigate through that
    code translating it into, say, SQL:

    ... WHERE Name IS NOT NULL AND Age > 21

    AFAIK, no other language has such feature. I wonder whether Go has any
    plans into that direction.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at May 11, 2014 at 2:34 pm

    On Sunday, May 11, 2014 5:23:18 AM UTC+3, Igor Gatis wrote:
    TL;DR: will Go ever have support for "code as data" such as C# has with
    Expression Trees?

    C# has this really nice feature called Expression Trees. Basically you can
    write code and it becomes available as data (in the form of an expression
    tree). Here is an example of expression trees using lambda:

    class Foo {
    public string Name;
    public int Age;
    }

    Expression<Func<Foo, bool>> exp = foo => foo.Name != null && foo.Age > 21;

    (for those unfamiliar with C# lambda, the above syntax creates a function
    which receives foo of type Foo and evaluates into a bool).

    C# compiler will compile the code above doing all type checking but
    instead of generating actual binary, it will translate that expression into
    an object that represents the code. One can later navigate through that
    code translating it into, say, SQL:

    ... WHERE Name IS NOT NULL AND Age > 21

    AFAIK, no other language has such feature.
    Lisp comes to mind. Depending what is the end representation, you can get
    the source for a function in javascript as well (function(){ return "hello"
    }).toString().

    I wonder whether Go has any plans into that direction.
    I doubt it will be added. There are too many different ways to extend a
    language and it's a major overhead needing to support all of them.

    What do you actually want to accomplish? The "Expression Tree" is just one
    solution for some set of problems... there are always other solutions to
    problems.

    + egon

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Khomoutov at May 11, 2014 at 4:47 pm
    On Sun, 11 May 2014 07:34:36 -0700 (PDT) egon wrote:
    [...]
    class Foo {
    public string Name;
    public int Age;
    }

    Expression<Func<Foo, bool>> exp = foo => foo.Name != null &&
    foo.Age > 21;

    (for those unfamiliar with C# lambda, the above syntax creates a
    function which receives foo of type Foo and evaluates into a bool).

    C# compiler will compile the code above doing all type checking but
    instead of generating actual binary, it will translate that
    expression into an object that represents the code. One can later
    navigate through that code translating it into, say, SQL:

    ... WHERE Name IS NOT NULL AND Age > 21
    [...]
    What do you actually want to accomplish? The "Expression Tree" is
    just one solution for some set of problems... there are always other
    solutions to problems.
    LINQ <http://msdn.microsoft.com/en-us/library/bb397926>

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at May 11, 2014 at 6:19 pm

    On Sunday, May 11, 2014 7:46:56 PM UTC+3, Konstantin Khomoutov wrote:
    On Sun, 11 May 2014 07:34:36 -0700 (PDT)
    egon <egon...@gmail.com <javascript:>> wrote:

    [...]
    class Foo {
    public string Name;
    public int Age;
    }

    Expression<Func<Foo, bool>> exp = foo => foo.Name != null &&
    foo.Age > 21;

    (for those unfamiliar with C# lambda, the above syntax creates a
    function which receives foo of type Foo and evaluates into a bool).

    C# compiler will compile the code above doing all type checking but
    instead of generating actual binary, it will translate that
    expression into an object that represents the code. One can later
    navigate through that code translating it into, say, SQL:

    ... WHERE Name IS NOT NULL AND Age > 21
    [...]
    What do you actually want to accomplish? The "Expression Tree" is
    just one solution for some set of problems... there are always other
    solutions to problems.
    LINQ <http://msdn.microsoft.com/en-us/library/bb397926>
    LINQ is also a solution, not a problem.

    As an example "How can I do type-safe queries?" would be an engineering
    problem. A problem "How do I ensure that queries don't crash the system?"
    would be closer to a business problem. The business problems are usually
    the important parts in a system, how you exactly solve them,, less so.
    Starting with the business problem means you have a much wider range of
    possible solutions.

    + egon


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Khomoutov at May 11, 2014 at 5:19 pm

    On Sat, 10 May 2014 23:23:18 -0300 Igor Gatis wrote:

    TL;DR: will Go ever have support for "code as data" such as C# has
    with Expression Trees?
    [...]

    Not on this level, but Go implements a Go parser in its own standard
    library, and the packages comprising this support are modular [1],
    including specific support for abstract syntax trees of Go programs
    (see `go/ast`).

    This gets you quite closer to LINQ -- if you are okay with playing by
    Go's rules (note that Go is way more low-level than C#). You won't
    have direct (compiler-level) support for turning a function (anonymous
    or otherwise) into an expression tree but I can imagine at least two
    ways to use this functionality:

    * You can parse a user-supplied text into a syntax tree at runtime
       and then feed the resulting AST into a specific driver which will
       further transform it to match its respective data model or
       use the AST directly (by using an evaluator).

    * Employ "bootstrapping" where you implement a pre-compilation step
       which takes some input and produces a file or a set of files with
       Go code (or several versions of it -- for instance, targetting
       different data access backends) which are then compiled normally.
       Such a bootstrapping program is usually executed by calling `go run`
       on it, which compiles the program, executes it and then throws it
       away.

    1. http://golang.org/pkg/go/

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 11, '14 at 2:23a
activeMay 11, '14 at 6:19p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase