Features of the Arguments

Objectives:

  • Implement function with default values of arguments.
  • Specify arguments by name in the function call.

Python

def foo(bar=True, baz=123, qux="abc"):
    print(f"bar={bar}, baz={baz}, qux={qux}")
    
foo()
foo(qux="hello")
foo(qux="hello", bar=True, baz=987)
foo(False, qux="hello", baz=987)

Additionally Python also supports:

  • Variadic positional args.
  • Variadic keyword args.
  • Keyword-only args.

Rust

Example shows how to implement keyword arguments, but positional arguments are not available at the same time.

#[derive(Default)]
struct Foo {
    bar: bool,
    baz: i32,
    qux: String,
}

impl Foo {
    fn default() -> Self {
        Self{
            baz: 999,
            ..Default::default()
        }
    }

    fn bar(mut self, val: bool) -> Self {
        self.bar = val;
        self
    }
    
    fn baz(mut self, val: i32) -> Self {
        self.baz = val;
        self
    }
    
    fn qux(mut self, val: String) -> Self {
        self.qux = val;
        self
    }
    
    fn call(&self) {
        println!("bar={}, baz={}, qux={}", self.bar, self.baz, self.qux);
    }
}

fn main() {
    Foo{bar: true, baz: 123, qux: "abc".to_string()}.call();
    Foo{bar: true, ..Default::default()}.call();
    Foo::default().call();
    Foo::default().qux("hello".to_string()).call();
    Foo::default().qux("hello".to_string()).bar(true).baz(987).call();
}

Notes:

  • default(), call() and each setter needs to be called (builder pattern).
  • Builder can also implement variadic args (by appending values to a vector) and variadic keyword args (by appending name and values to a hashmap).
  • Separate names can be used externally and internally (e.g. field in Foo can have a different name than corresponding setter).

Crystal

def foo(bar = true, baz = 123, qux = "abc")
  puts "bar=#{bar}, baz=#{baz}, qux=#{qux}"
end

foo()
foo(qux: "hello")
foo(qux: "hello", bar: true, baz: 987)
foo(false, qux: "hello", baz: 987)

Additionally Crystal supports:

  • Variadic positional args.
  • Variadic keyword args.
  • Keyword-only args.
  • Internal/external arg names.