Coder Social home page Coder Social logo

Comments (9)

abduld avatar abduld commented on May 11, 2024 3

The basic idea is that your set_x and set_y need to return a new instance of the class, since you are chaining the method calls. Try


struct Line:
    var x: fn (F32) -> F32
    var y: fn (F32) -> F32

    fn __init__(inout self):
        self.x = constant
        self.y = constant

    fn __init__(inout self, x: fn (F32) -> F32, y: fn (F32) -> F32):
        self.x = x
        self.y = y

    fn set_x(self, x: fn (F32) -> F32) -> Self:
        return Self(x, self.y)

    fn set_y(self, y: fn (F32) -> F32) -> Self:
        return Self(self.x, y)

    fn evaluate(self, points: DynamicVector[Vec2]) -> DynamicVector[Vec2]:
        var out = DynamicVector[Vec2]()
        for i in range(points.__len__()):
            let p = points[i]
            let x = p[0]
            let y = p[1]
            out.push_back(Vec2(self.x(x), self.y(y)))
        return out

which should allow you to write

    let e = Line().set_x(my_x).set_y(constant).evaluate(points)

from mojo.

abduld avatar abduld commented on May 11, 2024 2

This is an interesting and useful pattern. We will make sure we have an example of how to do this in Mojo ASAP

from mojo.

lsh avatar lsh commented on May 11, 2024

This also brings up the question: how are closures stored on a struct? In Rust, one would likely Box the closure. Would this allocate in Mojo to store the closure?

from mojo.

lsh avatar lsh commented on May 11, 2024

Thanks for looking into it! That makes sense. I wonder if there could be a self^ which moves self into a method

from mojo.

nmsmith avatar nmsmith commented on May 11, 2024

Why not allow r-values to be mutated in this way? Is it not possible to come up with a semantics for this?

Oh wait, I see: the original definition of set_x isn't returning anything.

I don't understand why set_x can't be written as follows, though:

fn set_x(inout self, x: fn (F32) -> F32) -> Self:
    self.x = x
    return self

from mojo.

sa- avatar sa- commented on May 11, 2024

The basic idea is that your set_x and set_y need to return a new instance

This is a lot like scala's copy function on a case class which creates a shallow copy.

See "Copying" for more details here: https://docs.scala-lang.org/tour/case-classes.html

from mojo.

lsh avatar lsh commented on May 11, 2024

I was trying to trigger moves rather than copies in this case, but I'm not sure that's possible right now

from mojo.

Mogball avatar Mogball commented on May 11, 2024

@lsh there's a typo in your code

    fn set_y(self&, y: fn(F32) -> F32):
        self.x = y

from mojo.

Mogball avatar Mogball commented on May 11, 2024

Closing this for now.

from mojo.

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.