You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fairly early on, I ran into a problem due to lack of type classes. So far, I've only needed the equivalent of type classes with a single function. I've worked around this by defining an alias for the function signature, manually making sure that the would-be type class instances have the same function signature, and then referring to them by the alias.
As an example, the book describes a generic Hittable class with a single function hit. It then describes the Sphere class, which implements Hittable, and describes the world as a HittableList.
# Function must be capturing because it is a struct method and closes# over the struct instance.aliasHittable=fn (Ray3, Interval, /) capturing-> HitRecord
"""A function signature for a hittable object."""
However, that doesn't work. I get the following error:
/Users/connorbaker/Packages/mojo_rt/main.mojo:17:14: error: invalid call to 'add': method argument #0 cannot be converted from unknown overload to 'fn(Ray3, Interval, /) capturing -> HitRecord'
world.add(green_horizon.hit)
~~~~~~~~~^~~~~~~~~~~~~~~~~~~
/Users/connorbaker/Packages/mojo_rt/main.mojo:17:15: note: try resolving the overloaded function first
world.add(green_horizon.hit)
^~~~~~~~~~~~~~~~~
Included from /Users/connorbaker/Packages/mojo_rt/main.mojo:3:
Included from /Users/connorbaker/Packages/mojo_rt/mojo_rt/camera.mojo:3:
/Users/connorbaker/Packages/mojo_rt/mojo_rt/hittable_list.mojo:16:5: note: function declared here
fn add(inout self, h: Hittable) -> None:
^
/Users/connorbaker/Packages/mojo_rt/main.mojo:18:14: error: invalid call to 'add': method argument #0 cannot be converted from unknown overload to 'fn(Ray3, Interval, /) capturing -> HitRecord'
world.add(normal_mapped_sphere.hit)
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/connorbaker/Packages/mojo_rt/main.mojo:18:15: note: try resolving the overloaded function first
world.add(normal_mapped_sphere.hit)
^~~~~~~~~~~~~~~~~~~~~~~~
Included from /Users/connorbaker/Packages/mojo_rt/main.mojo:3:
Included from /Users/connorbaker/Packages/mojo_rt/mojo_rt/camera.mojo:3:
/Users/connorbaker/Packages/mojo_rt/mojo_rt/hittable_list.mojo:16:5: note: function declared here
fn add(inout self, h: Hittable) -> None:
^
mojo: error: failed to parse the provided Mojo
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello all! I'm using Mojo to follow along with the Ray Tracing in One Weekend series: https://github.com/ConnorBaker/mojo_rt.
Background
Fairly early on, I ran into a problem due to lack of type classes. So far, I've only needed the equivalent of type classes with a single function. I've worked around this by defining an alias for the function signature, manually making sure that the would-be type class instances have the same function signature, and then referring to them by the alias.
As an example, the book describes a generic
Hittable
class with a single functionhit
. It then describes theSphere
class, which implementsHittable
, and describes theworld
as aHittableList
.In Mojo,
Hittable
is a function signature:Then,
Sphere
is astruct
with a methodhit
:And our
HittableList
is a wrapper aroundDynamicVector[Hittable]
:Problem
I'd like to construct my
world
like so:However, that doesn't work. I get the following error:
I find it odd that the following does work:
My
setup_world
function is here: https://github.com/ConnorBaker/mojo_rt/blob/9602d9d86c11ac8249806a421ef7cac47c8a7665/main.mojo#L12-L31.Why does one variant work while the other doesn't?
Beta Was this translation helpful? Give feedback.
All reactions