Best way to separate method to multiple lines


#1

How do you keep method lines from going over 80 characters?


#2

Note that you can define methods as f(x) = ... for short one-liners (although even that can be broken over multiple lines via begin/end blocks, parentheses, etcetera), but for complicated methods that take multiple lines, you would normally use the more verbose syntax:

function f(x)
   ...
end

#3

Thank you!

This is going to sound dumb, but from a cursory reading of the docs, it sounds like methods and functions are distinct.


#4

They are distinct. But you define multiple methods for the same function just by defining different dispatches.

function f(x)
  ...
end 

function f(x,y)
  ...
end

those are different methods for the same function.


#5

Also note that if you define a callable with the same name in 2 different modules, e.g.:

module A
    f(x) = ...
end

module B
    f(x,y) = ...
end

then A.f() and B.f() not only define different methods, but also different functions. There’s a long discussion about it somewhere on the old forum, but in short it’s done to avoid mixing totally different concepts like e.g. Database.connect(host, port) and Facebook.connect(person1, person2). If you want, for example, B.f() to extend A.f(), you should do:

module A
    f(x) = ...
end

module B
    import A: f
    f(x,y) = ...
end

This way B will know that f comes from A and thus it should extend existing function instead of creating a new one.