Hacer que una struct cumpla un trait
Implemente los métodos que exige un trait.
Hacer que una struct cumpla un trait es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Signing the Contract
A trait only describes behavior. To use it, a struct must conform by actually implementing the required methods. ✍️
Naming the Trait
You declare conformance by listing the trait in parentheses after the struct name, just like a parameter list.
struct Dog(Greetable):
var name: StringImplement Every Method
Conforming means you must implement each method the trait requires, with a matching name and signature.
Matching the Signature
Your method has to match the trait's signature: same arguments and return type. A mismatch breaks conformance.
fn greet(self) -> String:
return "Woof from " + self.nameThe Compiler Verifies It
When you claim a trait, Mojo checks at compile time that every required method is present and correct.
Missing a Method Fails
Forget a required method and the build fails with a clear error. The contract is enforced, not just suggested.
Conform to Many Traits
A struct can satisfy several traits at once by listing them all, gaining each capability it implements.
struct Dog(Greetable, Stringable):
var name: StringBehavior Stays Yours
The trait says which methods exist, but your struct decides how they work. A Cat and Dog can greet differently.
Extra Methods Are Fine
A struct may add its own methods beyond the trait. Conformance only requires the contract be met, not limited.
Now It Qualifies
Once a struct conforms, it can be passed anywhere that trait is expected. That is the real payoff coming next.
A Concrete Example
Here a Dog conforms to Greetable and supplies its own greeting, fully satisfying the trait.
struct Dog(Greetable):
var name: String
fn greet(self) -> String:
return "Woof!"Quick Check
How does a struct promise to follow a trait?
Recap
To conform, list the trait after the struct name and implement its methods with matching signatures. The compiler checks it. ✅
Preguntas frecuentes
¿La lección «Hacer que una struct cumpla un trait» es gratis?
Sí — el texto completo de «Hacer que una struct cumpla un trait» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Hacer que una struct cumpla un trait»?
Implemente los métodos que exige un trait. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Mojo Academy?
No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Hacer que una struct cumpla un trait»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?
Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- ¿Qué es un trait?
- Hacer que una struct cumpla un trait
- Traits integrados como Copyable
- Funciones genéricas sobre traits