Passing functions and unknown overloads #1335
Unanswered
ConnorBaker
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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