Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure, but I find it less readable and more verbose than your usual constants.

Also you can't pattern match on a function return value IIRC. Or maybe I didn't find the right syntax, I get "invalid pattern in match" or "cannot invoke remote function inside a match" when pattern matching on function parameters.

There's also solutions with macros, but why complicate something so simple :)



Those are all good points.

When we talk about macros and function parameters, however, we want to avoid spreading multiple constants across multiple modules, because they lead to compile-time dependencies (and potentially long compilation cycles), so my go-to approach has been to define a single module with all of my constants (they could be macros or not, it changes per project).

I wonder if this could be a matter of promoting certain styles rather than adding language features?


> Also you can't pattern match on a function return value IIRC.

You definitely can. A function return value is just any other value and you can pattern match on it directly, or with the `case` or `with` constructs.


I think the poster means "you can't pattern match with a function value


I don't see how this would be useful.


What would be the correct way to pattern match here?

  def foo(<I want to pattern match the value returned by GreatModule.constant_substitute()>), do: "bar"
 
  def foo(value), do: value


Use a `case` in the body of the function. Why do you absolutely need to pattern match in the function header?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: