elixir-lang / elixir Goto Github PK
View Code? Open in Web Editor NEWElixir is a dynamic, functional language for building scalable and maintainable applications
Home Page: https://elixir-lang.org/
License: Apache License 2.0
Elixir is a dynamic, functional language for building scalable and maintainable applications
Home Page: https://elixir-lang.org/
License: Apache License 2.0
delete/2
fetch/2
fetch/3
store/3
update/3
update/4
This is insane stuff because I cannot imagine why someone would want to send a string or atom as a tuple or list index, but if it happens by accident, it will be very helpful to raise a consistent error.
Examples:
Tuple:
{1,2,3}["foo"] # => error 'badarg
{1,2,3}['foo] # => error {'nomethod,{'"+",1,'foo}}
List:
[1,2,3]['foo] # => error {'nomethod,{'"-",1,'foo}}
[1,2,3]["foo"] # => error {'nomethod,{'"-",1,"foo"}}
I believe it should always raise 'badarg for consistency
Similar to erlang's fun foo:bar/3
If I call this method I get an error. f.e. "lists".to_list will be return error about existing method. this's needed to check.
Below is the erlang version I'm using...
Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
... and the failing test:
1) utc_test(DateTimeTest)
error {'badmatch,{{2011,4,12},{22,14,7}}}
stacktrace:
DateTimeTest::Proto#utc_test/0
ExUnit#run_tests/4
ExUnit#run/2
ExUnit#run/0
erl_eval#do_apply/5
elixir#eval/5
Code::Init#-process_argv/2-lc$^0/1-0-/2
Code::Init#-process_argv/2-lc$^0/1-0-/2
Thanks!
format_stacktrace
and format_catch
needs to be tested.
This causes compile error:
begin
IO.puts result.inspect
rescue 'error: _
end
But this works:
begin
IO.puts result.inspect
rescue 'error: _
[]
end
I think this should work as if-else block and automatically yield []
elixir_module is too strict for old modules:
check_module_available(ElixirName) ->
try
ErrorInfo = elixir_constants:lookup(ElixirName, attributes),
[{ErrorFile,ErrorLine}] = proplists:get_value(exfile, ErrorInfo),
error({module_defined, {ElixirName, list_to_binary(ErrorFile), ErrorLine}})
catch
error:{no_module, _} -> []
end.
Proper procedure is to call
Erlang.code.soft_purge('ex + ElixirName) and then overload module.
If some features may give necessary power, but at the same time may lead to very complex code,
then usually people design interface to them harder then usual, but possible.
For example in Ruby, when you want to assign instance variable with custom name
then you should write very long method name (instance_variable_set
).
According to README.md require
recommened to use with only:
, why just don't make it by default?
Like:
require Orddict, [values: 1]
%% but:
require Orddict, import_all_to_scope: true
Something like that.
Another question: why there are two different methods for importing: require
and import
? Can they be combined?
Running archlinux, with erlang R14B, with elixir git commit 26bec31
Stdout and Stderr
Running Erlang tests ...
time erl -I include -noshell -pa ebin test/ebin -eval 'test_helper:test(), halt().'
=INFO REPORT==== 23-Mar-2011::22:23:12 ===
application: elixir
exited: {bad_return,
{{elixir,start,[normal,[]]},
{'EXIT',
{function_clause,
[{filename,do_flatten,
[<<"kernel/include/file.hrl">>,[]]},
{filename,split,1},
{'File::Mixin',split,2},
{'Record::Mixin',retrieve,3},
{'Record::Definition',record,3},
{erl_eval,do_apply,5},
{erl_eval,exprs,5},
{elixir_object,compile,8}]}}}}
type: temporary
All 210 tests passed.
1.03user 0.18system 0:02.06elapsed 58%CPU (0avgtext+0avgdata 68608maxresident)k
0inputs+0outputs (0major+7248minor)pagefaults 0swaps
Running Elixir tests ...
time bin/exunit test/elixir/*_test.ex
{"init terminating in do_boot",{{noconstant,'Code::Init'},[{elixir_errors,error,1},{elixir,start,0},{init,start_it,1},{init,start_em,1}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
real 0m2.502s
user 0m0.518s
sys 0m1.692s
make: *** [test_elixir] Error 1
We should rename:
ns
macro to module
__NAMESPACE__
to __MODULE__
src/elixir_namespace.erl
to src/elixir_module.erl
And any other reference we may find.
I found that unit test failed since de49179.
The error message is:
{"init terminating in do_boot",{{yecc_bug,"1.3",{missing_state_in_action_table,294}},[{elixir_parser,yeccpars2,7},{elixir_parser,yeccpars0,5},{elixir_transform,forms,3},{elixir_transform,parse_and_transform,3},{elixir,parse,3},{elixir,eval,3},{lists,foreach,2},{test_helper,test,0}]}}
510a79a is the last good commit.
./bin/elixir -v
{"init terminating in do_boot",{undef,[{elixir,start,[]},{init,start_it,1},{init,start_em,1}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
OSx 10.6.7
erl -v
Erlang R14B02 (erts-5.8.3) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
iex> a = [1, 2, [3, 4]]
[1,2,[3,4]]
iex> a[2][1]
** error
nofile:1: syntax error before: '['
elixir_errors#error/1
elixir_transform#parse/3
elixir#eval/5
IEX::Proto#loop/0
iex>
Wrapping the receiver in parens works fine (e.g. (a[2])[1]
).
(master))% erl
Erlang R14B03 (erts-5.8.4) [source] [smp:8:8] [rq:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.4 (abort with ^G)
1>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
a
(master))% make test
Compiling Erlang source ...
erlc -I include -W0 -o ebin src/*.erl
Compiling lexer ...
erl -I include -noshell -pa ebin -eval 'leex:file("src/elixir_lexer.xrl"), halt().'
Parsing file src/elixir_lexer.xrl, contained 69 rules.
NFA contains 1436 states, DFA contains 1459 states, minimised to 644 states.
Writing file src/elixir_lexer.erl, open error
erlc -I include -W0 -o ebin src/elixir_lexer.erl
src/elixir_lexer.erl:none: no such file or directory
make: *** [src/elixir_lexer.erl] Error 1
It appears to fail to write out the lexer after it gets generated. But I even chmod'd the src dir to 0777 to make sure it wasn't a permissions problem and thats not it. Any clues?
Thanks
-Ezra
trying to follow the examples in the tutorial, i get this:
Interactive Elixir (0.3.1.dev)
Running on Erlang R14B01 (erts-5.8.2) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
iex> h
** error 'undef
IEX::Context#h(<#IEX::Context {}>)
erl_eval#do_apply/5
elixir#eval/5
IEX::Behavior#loop/0
iex> [h|t] = [1,2,3]
[1,2,3]
iex> h
** error {'unbound,'h}
erl_eval#expr/3
iex> i
** error 'undef
IEX::Context#i(<#IEX::Context {}>)
erl_eval#do_apply/5
elixir#eval/5
IEX::Behavior#loop/0
iex> [x|y] = [1,2,3]
[1,2,3]
iex> x
1
iex> y
[2,3]
iex>
is there anything special about h in iex?
And see how we can have better messages with backtraces (including Erlang R15).
Extracted from here: http://build.erlagner.org/browse/AGNER-NIGHTLYPKGS-JOB1-130/test/case/2527965
Compiling Erlang source ... erlc -I include -W0 -o ebin src/*.erl Compiling Elixir source ... erl -I include -noshell -pa ebin -s elixir_compiler core -s erlang halt {\"init terminating in do_boot\",{undef,[{elixir_lexer,string,[\"% elixir: cache object Object module Methods % Create a new object using the current object as parent % % ## Example % % obj = Object.new % obj.__parent__ %=> 'Object % % ## Notes % % The new method is special cased by the compiler to receive % all arguments wrapped into a single array. def new(args) Erlang.elixir_object_methods.new(self, args) end def mixin(module) Erlang.elixir_object_methods.mixin(self, module) end def proto(module) Erlang.elixir_object_methods.proto(self, module) end def __name__ Erlang.elixir_object_methods.name(self) end def __parent_name__ Erlang.elixir_object_methods.parent_name(self) end def __parent__ Erlang.elixir_object_methods.parent(self) end % Returns a list of atoms representing all mixins for the current object. % The mixins chain is retrieved by getting the current object mixins and % appending all protos from its parents. % % ## Example % % Imagine the following definition: % % object Foo % mixin SomeMethods % proto MoreMethods % end % % The mixins chain for Foo is: % % Foo.__mixins__ % => ['SomeMethods, 'Object::Methods] % % Where `SomeMethods` is a module mixed into `Foo` and the `Object::Methods` % is a module added as proto on `Foo` parent, which is the object `Object`: % % Foo.__parent__ % => 'Object % % The mixins for `Foo.new` are these: % % Foo.new.__mixins__ %=> ['MoreMethods, 'Object::Methods] % % Where `MoreMethods` is a module added as proto to `Foo.new` parent (which is `Foo`) % and `Object::Methods` is a proto on `Foo.new` grandparent (which is `Object`). % % If we added a mixin to `Foo.new`, we would have the following result: % % Foo.new.mixin(MuchMoreMethods).__mixins__ %=> ['MuchMoreMethods, 'MoreMethods, 'Object::Methods] % % In other words, calculating the mixins chain is as simple as: % % [self.exclusive_mixins, self.parent.exclusive_protos, self.parent.parent.exclusive_protos, ...] % % Until parent becomes empty (`Object` is the only object that does not have a parent). % % Notice that we don't have methods called `exclusive_mixins` and `exclusive_protos`, % they are mentioned just as examples. % % == Mixins inside object definitions % % In Elixir, all methods are carried in modules. Regular objects does not have methods per-se. % However, for convenience, Elixir allows you to define methods inside the object definition: % % object Foo % def bar % 'baz % end % end % % What happens internally is that Elixir automatically creates a module named `Foo::Proto` % and automatically adds it as `Foo` proto. In order for this to work properly, Elixir % adds `Module::Methods` as mixin during the object definition, but it is removed from % the mixins chain after the object is defined: % % object Foo % __mixins__ % => ['Module::Methods, 'Object::Methods] % end % % Foo.__mixins__ % => ['Object::Methods] % def __mixins__ Erlang.elixir_object_methods.mixins(self) end % Returns a list of atoms representing all proto for the current object. % The mixins chain is retrieved by getting the current object protos and % appending all protos from its parents. % % ## Example % % Imagine the following definition: % % object Foo % mixin SomeMethods % proto MoreMethods % end % % The protos chain for Foo is: % % Foo.__protos__ % => ['MoreMethods, 'Object::Methods] % Foo.new.__protos__ % => ['MoreMethods, 'Object::Methods] % % The lookup for protos happens in a similar fashion to mixins: % % [self.exclusive_protos, self.parent.exclusive_protos, self.parent.parent.exclusive_protos, ...] % % Read the documentation for `__mixins__` for more information. def __protos__ Erlang.elixir_object_methods.protos(self) end def __ancestors__ Erlang.elixir_object_methods.ancestors(self) end def inspect name = __name__ if name != [] name.to_s else \\"<#{__parent_name__} #{get_ivars.inspect}>\\" end end def to_s self.inspect end def initialize self end def get_ivar(name) Erlang.elixir_object_methods.get_ivar(self, name) end % Returns a `Dict` with all variable names and values as its key-values. % % ## Example % % object Foo % def initialize % @('bar: 1, 'baz: 2) % end % end % % Foo.new.__ivars__ % => { 'bar: 1, 'baz: 2 } % def get_ivars OrderedDict.new Erlang.elixir_object_methods.data(self) end def set_ivar(name, value) Erlang.elixir_object_methods.set_ivar(self, name, value) end def set_ivars(value) Erlang.elixir_object_methods.set_ivars(self, value) end def update_ivar(name, fun) Erlang.elixir_object_methods.update_ivar(self, name, fun) end def update_ivar(name, initial, fun) Erlang.elixir_object_methods.update_ivar(self, name, initial, fun) end def __send__(method, args := []) Erlang.elixir_dispatch.dispatch(self, method, args) end % Those methods are related to methods introspection. def __proto_methods__ Erlang.elixir_methods.proto_methods(self) end def __mixin_methods__ Erlang.elixir_methods.mixin_methods(self) end % Exceptions related methods def __stacktrace__ filter_stacktrace Erlang.get_stacktrace end def error(reason) Erlang.error(reason) end def throw(reason) Erlang.throw(reason) end def exit(reason) Erlang.exit(reason) end def catch!(function) filter_catch_stacktrace Erlang.elixir_object_methods.function_catch(function) end def method_missing('initialize, args) IO.puts \\"[ELIXIR] Defining 'constructor' is deprecated. Please define 'initialize' and return an object.\\" @(__send__('constructor, args)) end def method_missing(method, args) error { 'nomethod, {self, method, Erlang.length(args)} } end % Set the following methods to private. Erlang.elixir_module_methods.set_visibility(self, 'private) def filter_stacktrace(stacktrace) filter_stacktrace(stacktrace, []) end def filter_stacktrace([{raw_module, function, raw_arity}|t], buffer) if filtered = filter_stacktrace_module(raw_module.to_char_list) module = filtered arity = if raw_arity.__parent_name__ == 'Integer raw_arity - 1 else raw_arity end else module = raw_module arity = raw_arity end filter_stacktrace t, [{module, function, arity}|buffer] end def filter_stacktrace([], buffer) buffer.reverse end def filter_stacktrace_module([$e, $x, h|t]) when h >= $A andalso h <= $Z Atom.from_char_list [h|t] end def filter_stacktrace_module(_) nil end def filter_catch_stacktrace({ 'EXIT, { reason, stacktrace } }) { 'EXIT, { reason, filter_stacktrace(stacktrace) } } end def filter_catch_stacktrace(other) other end end % Object::Methods is automatically mixed and proto'd by Elixir % runtime so we can bootstrap the object system. % mixin Object::Methods % proto Object::Methods end\",1]},{elixir_transform,forms,3},{elixir_transform,parse,3},{elixir,eval,5},{elixir_compiler,'-core/0-lc$^0/1-0-',1},{elixir_compiler,core,0},{init,start_it,1},{init,start_em,1}]}}\r \r Crash dump was written to: erl_crash.dump\r init terminating in do_boot ()\r make: *** [exbin] Error 1 ERROR: Build failed
When running following code:
module Iftest
def test
if true
IO.puts "if true works"
else
IO.puts "if true fails"
end
if false
IO.puts "if false fails"
else
IO.puts "if false works"
end
end
end
Iftest.test
It works properly, but it causes some Erlang warning messages:
src/../play/iftest2.ex:3: this clause cannot match because of different types/sizes
src/../play/iftest2.ex:9: this clause cannot match because of different types/sizes
However, if the if-statement is not in a function body, the warning message is not shown:
if true
IO.puts "if true works"
else
IO.puts "if true fails"
end
if false
IO.puts "if false fails"
else
IO.puts "if false works"
end
Sample code is available at my repository:
Using this bitmap file works fine in Erlang's REPL, but not Elixir's.
1> file:read_file("input.bmp").
{ok,<<66,77,54,4,4,0,0,0,0,0,54,4,0,0,40,0,0,0,0,2,0,0,0,
2,0,0,1,...>>}
iex> File.read("input.bmp")
** error 'badarg
*** ERROR: Shell process terminated! (^G to start new job) ***
=ERROR REPORT==== 16-May-2011::14:21:52 ===
Error in process <0.35.0> with exit value: {badarg,[{io,format,[standard_error,
<<263259 bytes>>,[]]},{lists,foreach,2},
{'exList::Proto',each,2},{'exIEX::Proto',loop,1}]}
iex> Erlang.file.read_file("input.bmp")
** error 'badarg
*** ERROR: Shell process terminated! (^G to start new job) ***
=ERROR REPORT==== 16-May-2011::14:23:19 ===
Error in process <0.35.0> with exit value: {badarg,[{io,format,[standard_error,
<<263265 bytes>>,[]]},{lists,foreach,2},{'exList::Proto',each,2},{'exIEX::Proto',loop,1}]}
I suspect that this is because iex
is not sanitizing the strings that it presents on the console, and that somehow these values are being interpreted as printable characters? Possible fix may be to have a read option that returns a Bit String rather than a regular String?
many erlang functions accepts list of options (usually it's list of tupples). i think, that this is not cool. now we should use list of tuples too or funcs with default args, but in this case there is constant order of args, so it's not good too.
named args can solve this problem.
import
use
attribute(K,V)
Code sample:
for ({ verb, extra } in right) do
contents = quote do
defp unquote(name).(unquote(verb), [], dict) do
{ :ok, unquote(extra), dict }
end
end
Module.eval_quoted module, contents, [], __FILE__, __LINE__
end
They should be the same as element/setelement but using the proper parameters order. They need to be macros so they can still be used in guards.
Hi There,
I love the idea of elixir. But I really really hate the "end" statements. It's ugly, long and meaningless. How about some python style syntax like ifn? It would shorten the code by 20 to 30% in many cases and improve readability.
Eric
They need to be specials and properly unquoted in macros as __MODULE__
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.