2015-11-19 6 views
6

bir harita tanımlamanın 2 farklı sözdizimi mevcuttur: KullanılmasıElixir'de bir harita oluştururken neden farklı notasyonlar kullanamıyorum?

map = %{:a => 1, :b => 2} 
#=> %{a: 1, b: 2} 
map = %{a: 1, b: 2} 
#=> %{a: 1, b: '2} 

hem bir harita tanımlayan çalışırken aşağıdaki gibi:

map = %{a: 1, :b => 2} 
#=> ** (SyntaxError) iex:37: syntax error before: b 
:

map = %{:a => 1, b: 2} 
#=> %{a: 1, b: 2} 

Ama diğer sırayla kullanılan bir hata atar

Neden?

DÜZENLEME

OS: Ubuntu 15.4

İksir: Belki tutarlılık uğruna

+2

Bu olabilir Elixir'de böcek. –

+0

Bazı ayrıntıları eklemek isteyebilirsiniz. Elixir, OS sürümü vb. –

cevap

3

my issue on Github'a göre (aslında açmamam gereken), bu bir hata değildir.

İlk cevap (Ben gerçekten alamadım olan):

It's not a bug, it's the same syntax sugar that is used for keywords on the last argument of a function.

foo(bar, baz: 0, boz: 1) #=> foo(bar, [baz: 0, boz: 1]) 

The map syntax is represented as function call in the AST:

iex(1)> quote do: foo(bar, baz: 0, boz: 1) 
{:foo, [], [{:bar, [], Elixir}, [baz: 0, boz: 1]]} 
iex(2)> quote do: %{baz: 0, boz: 1} 
{:%{}, [], [baz: 0, boz: 1]} 

That's why the map keyword syntax only works for the last (or only) argument.

Ve anladım düşünüyorum bir anlamda Tamam kulağa ikinci cevap,:

Simple answer: b: 2 is syntax sugar for [b: 2] , but the sugar only works when it is at the end of a function call or "construct" such as %{} .

0

1.1.1? Bir flip-flop ve bir goth botu ile yürümek gibi. Fantezi görünebilir, yine de son derece rahatsız edici.