Elixir:何时使用.ex以及何时使用.exs文件 [英] Elixir: When to use .ex and when .exs files

查看:233
本文介绍了Elixir:何时使用.ex以及何时使用.exs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Elixir的文档状态指出

Elixir's documentation states that


除了Elixir文件扩展名.ex之外,Elixir还支持
.exs文件进行脚本编写。 Elixir用完全相同的
方式对待两个文件,唯一的区别在于意图。 .ex文件用于
编译,而.exs文件用于脚本编写,而无需
编译。

In addition to the Elixir file extension .ex, Elixir also supports .exs files for scripting. Elixir treats both files exactly the same way, the only difference is in intention. .ex files are meant to be compiled while .exs files are used for scripting, without the need for compilation.

但是我仍然不确定何时使用哪种文件类型。 .ex和.exs的缺点是什么?

But I'm still not sure when to use which file type. What are the downsides and the purpose of .ex and .exs?

推荐答案

.ex 用于编译代码, .exs 用于解释代码。

.ex is for compiled code, .exs is for interpreted code.

ExUnit测试,例如,包含在 .exs 文件中,因此您不必在每次更改测试时都重新编译。如果要编写脚本或测试,请使用 .exs 文件。否则,只需使用 .ex 文件并编译您的代码即可。

ExUnit tests, for example, are in .exs files so that you don't have to recompile every time you make a change to your tests. If you're writing scripts or tests, use .exs files. Otherwise, just use .ex files and compile your code.

对于优点/缺点,需要进行解释执行时间更长(如长生不老药必须解析,标记化等),但不需要编译即可运行。就是这样-如果运行脚本的灵活性比优化执行时间更重要,请使用 .exs 。在大多数情况下,您会使用 .ex

As far as pros/cons, interpretation will take longer to execute (as elixir has to parse, tokenize, etc.), but doesn't require compilation to run. That's pretty much it - if the flexibility of running scripts is more important than optimized execution time, use .exs. Most of the time, you'll use .ex.

这篇关于Elixir:何时使用.ex以及何时使用.exs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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