Comments (9)
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.
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.
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.
Thanks for looking into it! That makes sense. I wonder if there could be a self^
which moves self into a method
from mojo.
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.
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.
I was trying to trigger moves rather than copies in this case, but I'm not sure that's possible right now
from mojo.
@lsh there's a typo in your code
fn set_y(self&, y: fn(F32) -> F32):
self.x = y
from mojo.
Closing this for now.
from mojo.
Related Issues (20)
- [BUG] Program crash with mojo 24.3.0 (9882e19d) HOT 2
- [stdlib] Refactor uses of `StaticTuple` to use `InlineArray` HOT 3
- [stdlib] Audit uses of `StaticIntTuple` HOT 8
- [Feature Request] Implement deprecation warning as compile time warning or docstring standard
- [Docs] Incomplete code example for StringLiterals HOT 2
- [BUG] check-license failing sometimes HOT 6
- [BUG] Functions, traits, structs and aliases are leaking into builtins and are importable from anywhere HOT 2
- [Feature Request] Support for errno in external_call to Improve Error Handling HOT 2
- [BUG] `__call_location().file_name` returns incorrect information HOT 1
- [Feature Request] Make higher-order functions accepting non-capturing function as capturing function parameter
- [BUG] `__getitem__(self, Slice)` with `__refitem__` gives callee expects 2 parameters, but 0 were specified
- [BUG] alias using `__refitem__` fails to compile with "value doesn't have a memory type"
- [Feature Request] Support Dict set operation
- [Feature Request] Support `from_keys` for Dict
- [Feature Request] Constrained[False], or alternative, should not cause a return expected error HOT 1
- [BUG] Mojo SDK post-install test "`mojo repl`" failed HOT 1
- [Feature Request] Use Tuple[K, V] instead of DictEntry[K, V] HOT 1
- [Feature Request] DX: Change the default branch of modularml/mojo from `main` to `nightly` HOT 3
- [Docs] Mojo style guide for value parameters HOT 3
- [BUG] Weird auto-formatting for lines with a trailing comment
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mojo.