Logo

Programming-Idioms

  • Rust
  • Ruby
  • Pascal

Idiom #225 Declare and use an optional argument

Declare an optional integer argument x to procedure f, printing out "Present" and its value if it is present, "Not present" otherwise

procedure f; overload;
begin
  writeln('not present');
end;

procedure f(x: integer); overload;
begin
  writeln('present');
end;

Not exactly what tkoenig meant, but can be resolved using overloading (same procedure name with different parameter list) quite easily.
fn f(x: Option<()>) {
    match x {
        Some(x) => println!("Present {}", x),
        None => println!("Not present"),
    }
}
def f( x=nil )
  puts x ? "present" : "not present"
end
(defn f [& [x]]
  (if (integer? x)
    (println "Present" x)
    (println "Not present")))

Optional argument with positional parameters

New implementation...
< >
tkoenig