Code Monkey home page Code Monkey logo

Comments (2)

sadir avatar sadir commented on August 28, 2024 1

Yep! Perfect sense thank you. It doesn't cover both /3 options, and d will be the default value of 2 unless you supply all four arguments, because elixir matches to the left. I have a Ruby background so this is in line with my assumptions. Thanks for your help!

from elixir-in-action.

sasa1977 avatar sasa1977 commented on August 28, 2024

Good question! The best way to be sure is to verify it ;) Let's change the function to print each args:

defmodule MyModule do
  def fun(a, b \\ 1, c, d \\ 2) do
    IO.puts "a = #{a}"
    IO.puts "b = #{b}"
    IO.puts "c = #{c}"
    IO.puts "d = #{d}"
  end
end

$ iex test.exs

iex> MyModule.fun(:arg_1, :arg_2, :arg_3)
a = arg_1
b = arg_2
c = arg_3
d = 2

If you think about it, it makes sense. If a single default arg is omitted when calling a fun, then the right-most arg with a default (in our case the arg d) will have the default value.

Does that make sense?

from elixir-in-action.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.