Elixir-函数定义的语法是什么? [英] Elixir - What is syntax for function definitions?

查看:136
本文介绍了Elixir-函数定义的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OS XI上使用Elixir V1.1.1时,我难以理解Elixir函数定义语法。

Using Elixir V1.1.1 on OS X I am having problems getting my head around the Elixir function definition syntax.

给出一个函数定义:

def foobar(arg1, arg2), do: <<something>> end

在某些情况下,似乎需要使用括号后的逗号,而在其他情况下则不需要。同样, do之后的冒号和结束 end之后的冒号。我敢肯定我的情况过于复杂,但这似乎是一种编程语言的怪异设置。

It seems that in some cases the comma after the parenthesis is required, other times it is not. Likewise the colon after the 'do' and likewise the closing 'end'. I am sure I am vastly over-complicating the situation, but this seems like a weird setup for a programming language.

在什么情况下这些元素是必需的/可选的?

Under what circumstances are these elements required/optional?

推荐答案

您在此处组合了两种形式的方法定义-单行语法较短,而较长做 / end 语法。

You've combined two forms of method definition there - the shorter one-line syntax and the longer do/end syntax.

如果您使用单行方法,则可以可以这样写:

If you have a one-line method, you can write like the following:

def foobar(foo,bar),do:baz

注意逗号,没有结束

如果您有一个多行方法,语法略有不同:

If you have a multi-line method, the syntax is slightly different:

def foobar(foo, bar) do
  foo
  bar
end

请注意不要用逗号和 end 。

这两种类型均在此处描述: http:// elixir- lang.org/getting-started/modules.html#named-functions (请注意 Math.zero?/1 的长格式,以及功能捕获标题)

Both types are described here: http://elixir-lang.org/getting-started/modules.html#named-functions (note the long form for Math.zero?/1, and the shorter form under the 'Function capturing' heading)

这篇关于Elixir-函数定义的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆