Pipe operator |> for fluent procedure calls #5538
Closed
ScriptedWave
started this conversation in
Ideas/Requests
Replies: 2 comments 1 reply
-
|
This looks like something that would be in c++ |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Never going to happen. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I really enjoy the syntax of Odin and appreciate the Golang like feeling of many of the elements, but something that I have missed coming from go are methods and the clean .method() syntax. I completely understand why methods weren't included in Odin, as they go against many core principles of the language, such as preferring to be explicit.
Hence, I want to offer a more explicit alternative, stolen from functional languages like elixir and gleam, the |> operator. Although included in mainly functional languages, the pipe operator is not inherently functional, since it is just syntactic sugar for passing the expression before the operator into the following procedure. For example:
The main use case for this operator is for seamlessly chaining function calls, where normaly Odin requires you to make multiple nested procedures eg.
fmt.println(strings.to_lower(strings.trim_space(" Hello, World! "))). This is somewhat difficult to read and work with. With the pipe operator, that statement could look like" Hello, World! " |> strings.trim_space() |> strings.to_lower() |> fmt.println(). This statement is far more intuitive, and structures the statement as if it consisted of methods while still remaining explicit.To implement this operator, I would have it simply take the expression before it, and pass it into the following procedure as the first argument, nothing else.
Beta Was this translation helpful? Give feedback.
All reactions