Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm not sure this is true? Or I misinterpret.

    Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]
    Interactive Elixir (1.17.1) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> defmodule X, do: defstruct [:a]
    {:module, X,
     <<70, 79, 82, 49, 0, 0, 7, 128, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 244,
       0, 0, 0, 22, 8, 69, 108, 105, 120, 105, 114, 46, 88, 8, 95, 95, 105, 110,
       102, 111, 95, 95, 10, 97, 116, 116, 114, ...>>, %X{a: nil}}
    iex(2)> x = %X{a: 1}
    %X{a: 1}
    iex(3)> x[:a]
    ** (UndefinedFunctionError) function X.fetch/2 is undefined (X does not implement the Access behaviour

    You can use the "struct.field" syntax to access struct fields. You can also use Access.key!/1 to access struct fields dynamically inside get_in/put_in/update_in)
        X.fetch(%X{a: 1}, :a)
        (elixir 1.17.1) lib/access.ex:322: Access.get/3
        iex:7: (file)
    iex(4)> get_in(x, [:a])
    ** (UndefinedFunctionError) function X.fetch/2 is undefined (X does not implement the Access behaviour

    You can use the "struct.field" syntax to access struct fields. You can also use Access.key!/1 to access struct fields dynamically inside get_in/put_in/update_in)
        X.fetch(%X{a: 1}, :a)
        (elixir 1.17.1) lib/access.ex:322: Access.get/3
        iex:7: (file)
    iex(5)> get_in(x, [Access.key!(:a)])
    1


    get_in(x.a)
better example would be nested.

    get_in(x.a.b.c)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: