M
Modular3mo ago
Sukiru

def in Mojo

When writing a program from scratch, what does def bring to the table over fn? Although def has its benefits and would work well alongside fn, in practice, I don't see what stops it from being viewed as "legacy Python"
4 Replies
benny
benny3mo ago
IMO you are correct, def is just worse fn. It can help with code quality if you are using Python paradigms or functions and want to avoid having to specify types, lifetimes, etc. but you are realistically going to end up with a slower and less type safe function in the end. Generally I try to only use fn, but I do not work with Python like code that frequently, it could be different for you.
bunny
bunny3mo ago
I'd add that if I'm doing a "py to mojo" refactor, then I anticipate using def as a stepping-stone and a real life-saver to speed up the refactor. But the Rust-like fn is what I see as the goal -- all fn all the time.
Sukiru
Sukiru3mo ago
Yup. I agree with that. However, Chris mentioned not to view it as "legacy Python" and to not interpret "fn to be mojo" so that's where this is coming from Even with hefty Python codebases, I don't see much practical use other than for the sake of compatibility for those that want to remain mostly unfamiliar with fn. At the end, I see myself using fn the vast majority of the time and hence "fn as mojo"
sora
sora3mo ago
I think most library code should stay in fn land. However, it is indeed very convenient to have "dynamic shell" around your "static core", and defs grant you that.