Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
¿Raku tiene el tipo de unión de Python?

En Python, Python tiene un tipo de unión , lo cual es conveniente cuando un método puede aceptar varios tipos:

 from typing import Union def test(x: Union[str,int,float,]): print(x) if __name__ == '__main__': test(1) test('str') test(3.1415926)

Raku probablemente no tenga el tipo Unión como Python, pero una cláusula where puede lograr un efecto similar:

 sub test(\x where * ~~ Int | Str | Rat) { say(x) } sub MAIN() { test(1); test('str'); test(3.1415926); }

Me pregunto si Raku tiene la posibilidad de proporcionar el tipo Union como Python.

 # vvvvvvvvvvvvvvvvvvvv - the Union type doesn't exist in Raku now. sub test(Union[Int, Str, Rat] \x) { say(x) }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Mi respuesta (que es muy similar a su primera solución;) sería:

 subset Union where Int | Rat | Str; sub test(Union \x) { say(x) } sub MAIN() { test(1); test('str'); test(pi); } Constraint type check failed in binding to parameter 'x'; expected Union but got Num (3.141592653589793e0)

(o puede poner una cláusula where en la firma de llamada, como la tiene)

En contraste con Python:

  • esto es nativo en raku y no depende de un paquete como "escribir" para ser importado
  • Python Union / SumTypes se utilizan para sugerencias estáticas, lo cual es bueno, por ejemplo. IDE
  • pero estos tipos no se aplican en Python (según el comentario de @freshpaste y este SO ), en raku están marcados y fallarán en el tiempo de ejecución

Entonces, la sintaxis de raku está ahí para hacer lo que pides... claro, es un idioma diferente, por lo que lo hace de una manera diferente.

Personalmente, creo que un lenguaje escrito debería fallar si se infringen las comprobaciones de tipo. Me parece que el tipo de insinuación que no siempre se aplica es un falso manto de comodidad.

En un punto más amplio, raku también ofrece tipos Allomorph incorporados para IntStr, RatStr, NumStr y ComplexStr, para que pueda trabajar en un modo mixto utilizando funciones de cadena y matemáticas.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!