Elixir中的常量


Learn X By Example · Elixir 篇 · Constants in Elixir


以下程序展示了常量的使用。

defmodule Constants do
  def print_constants do
    # Declares a constant value
    s = "constant"
    IO.puts(s)

    # A const statement can appear anywhere a var statement can
    n = 500_000_000

    # Constant expressions perform arithmetic with arbitrary precision
    d = 3.0e20 / n
    IO.puts(d)

    # A numeric constant has no type until it’s given one, such as by an explicit conversion
    IO.puts(trunc(d))

    # A number can be given a type by using it in a context that requires one, such as a variable
    # assignment or function call. For example, here math.sin expects a float
    IO.puts(:math.sin(n))
  end
end

Constants.print_constants()

将代码保存到扩展名为 .exs 的文件中,然后使用 Elixir 解释器运行。

$ elixir constants.exs
constant
6.0e+11
600000000000
-0.28470407323754404

这也太简单了,下一个,下一个。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值