Skip to content

Commit

Permalink
Rename some core::option functions
Browse files Browse the repository at this point in the history
from_maybe => get_with_default
maybe => with_option
may => with_option_do

I know these names are kind of ridiculous, but it's the best I could think of.
Feel free to bikeshed. Closes #2081
  • Loading branch information
catamorphism committed Apr 2, 2012
1 parent 987bc23 commit 21be137
Show file tree
Hide file tree
Showing 31 changed files with 67 additions and 64 deletions.
2 changes: 1 addition & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ programs that just can't be typed.

~~~~
let n = option::none;
# option::may(n, fn&(&&x:int) {})
# option::with_option_do(n, fn&(&&x:int) {})
~~~~

If you never do anything else with `n`, the compiler will not be able
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn load_props(testfile: str) -> test_props {
pp_exact = parse_pp_exact(ln, testfile);
}

option::may(parse_aux_build(ln)) {|ab|
option::with_option_do(parse_aux_build(ln)) {|ab|
aux_builds += [ab];
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<A> of iterable<A> for [A] {

impl<A> of iterable<A> for option<A> {
fn iter(blk: fn(A)) {
option::may(self, blk)
option::with_option_do(self, blk)
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ pure fn is_some<T>(opt: option<T>) -> bool {
!is_none(opt)
}

pure fn from_maybe<T: copy>(opt: option<T>, def: T) -> T {
pure fn get_or_default<T: copy>(opt: option<T>, def: T) -> T {
#[doc = "Returns the contained value or a default"];

alt opt { some(x) { x } none { def } }
}

fn maybe<T, U: copy>(opt: option<T>, def: U, f: fn(T) -> U) -> U {
fn with_option<T, U: copy>(opt: option<T>, def: U, f: fn(T) -> U) -> U {
#[doc = "Applies a function to the contained value or returns a default"];

alt opt { none { def } some(t) { f(t) } }
}

fn may<T>(opt: option<T>, f: fn(T)) {
fn with_option_do<T>(opt: option<T>, f: fn(T)) {

This comment has been minimized.

Copy link
@marijnh

marijnh Apr 6, 2012

Contributor

So, eh, I don't think this is much of an improvement. may was cryptic, sure, but this is an extemely common operation, and making users spell out a 13-character name for it seems unfortunate. How about with, when, or even may?

This comment has been minimized.

Copy link
@catamorphism

catamorphism Apr 6, 2012

Author Contributor

@brson said (in comments on #2081) that he didn't like may -- "They all just read to me as 'this is a function that might do something' - great."

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Apr 6, 2012

Contributor

My opinion:

  • may() becomes iter(): exactly as though option were a 0- or 1-element vector.
  • get_with_default() becomes just default() or perhaps getd(). get_with_default(), while explicit, is just too long.
  • with_option() is just removed altogether. Just do foo.map(f).default(d)

This comment has been minimized.

Copy link
@catamorphism

catamorphism Apr 6, 2012

Author Contributor

@nikomatsakis -- The first two suggestions are ok with me, but I disagree with the third. In Haskell, I use the maybe function all the time. If we're going for short names, making people write out two function calls (or making everyone define their own equivalent function) doesn't seem like the right way to me.

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Apr 6, 2012

Contributor

perhaps get_dflt() and map_dflt()? or spell it out :)

This comment has been minimized.

Copy link
@catamorphism

catamorphism Apr 6, 2012

Author Contributor

Wh nds vwls? ;-) Seriously, though, I like that, if spelled out.

#[doc = "Performs an operation on the contained value or does nothing"];

alt opt { none { } some(t) { f(t); } }
Expand Down Expand Up @@ -94,11 +94,12 @@ impl extensions<T:copy> for option<T> {
"]
fn chain<U>(f: fn(T) -> option<U>) -> option<U> { chain(self, f) }
#[doc = "Returns the contained value or a default"]
fn from_maybe(def: T) -> T { from_maybe(self, def) }
fn get_or_default(def: T) -> T { get_or_default(self, def) }
#[doc = "Applies a function to the contained value or returns a default"]
fn maybe<U: copy>(def: U, f: fn(T) -> U) -> U { maybe(self, def, f) }
fn with_option<U: copy>(def: U, f: fn(T) -> U) -> U
{ with_option(self, def, f) }
#[doc = "Performs an operation on the contained value or does nothing"]
fn may(f: fn(T)) { may(self, f) }
fn with_option_do(f: fn(T)) { with_option_do(self, f) }
#[doc = "
Gets the value out of an option
Expand Down
7 changes: 4 additions & 3 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ mod tests {
setenv("HOME", "");
assert os::homedir() == none;

option::may(oldhome, {|s| setenv("HOME", s)});
option::with_option_do(oldhome, {|s| setenv("HOME", s)});
}

#[test]
Expand Down Expand Up @@ -732,8 +732,9 @@ mod tests {
setenv("USERPROFILE", "/home/PaloAlto");
assert os::homedir() == some("/home/MountainView");

option::may(oldhome, {|s| setenv("HOME", s)});
option::may(olduserprofile, {|s| setenv("USERPROFILE", s)});
option::with_option_do(oldhome, {|s| setenv("HOME", s)});
option::with_option_do(olduserprofile,
{|s| setenv("USERPROFILE", s)});
}

// Issue #712
Expand Down
7 changes: 3 additions & 4 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ If the result is an error
pure fn get<T: copy, U>(res: result<T, U>) -> T {
alt res {
ok(t) { t }
err(_) {
// FIXME: Serialize the error value
// and include it in the fail message (maybe just note it)
fail "get called on error result";
err(the_err) {
// FIXME: have a run-fail test for this
unchecked{ fail #fmt("get called on error result: %?", the_err); }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ fn spawn_raw(opts: task_opts, +f: fn~()) unsafe {
}
};

option::may(opts.notify_chan) {|c|
option::with_option_do(opts.notify_chan) {|c|
// FIXME (1087): Would like to do notification in Rust
rustrt::rust_task_config_notify(new_task, c);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustsyntax/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ fn highlight_lines(cm: codemap::codemap, sp: span,
}

fn print_macro_backtrace(cm: codemap::codemap, sp: span) {
option::may (sp.expn_info) {|ei|
let ss = option::maybe(ei.callie.span, "",
option::with_option_do (sp.expn_info) {|ei|
let ss = option::with_option(ei.callie.span, "",
bind codemap::span_to_str(_, cm));
print_diagnostic(ss, note,
#fmt("in expansion of #%s", ei.callie.name));
Expand Down
2 changes: 1 addition & 1 deletion src/librustsyntax/ext/qquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn expand_ast(ecx: ext_ctxt, _sp: span,
-> @ast::expr
{
let mut what = "expr";
option::may(arg) {|arg|
option::with_option_do(arg) {|arg|
let args: [@ast::expr] =
alt arg.node {
ast::expr_vec(elts, _) { elts }
Expand Down
2 changes: 1 addition & 1 deletion src/librustsyntax/parse/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn eval_crate_directives_to_mod(cx: ctx, cdirs: [@ast::crate_directive],
-> (ast::_mod, [ast::attribute]) {
#debug("eval crate prefix: %s", prefix);
#debug("eval crate suffix: %s",
option::from_maybe(suffix, "none"));
option::get_or_default(suffix, "none"));
let (cview_items, citems, cattrs)
= parse_companion_mod(cx, prefix, suffix);
let mut view_items: [@ast::view_item] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/librustsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ fn print_mac(s: ps, m: ast::mac) {
some(@{node: ast::expr_vec(_, _), _}) { }
_ { word(s.s, " "); }
}
option::may(arg, bind print_expr(s, _));
option::with_option_do(arg, bind print_expr(s, _));
// FIXME: extension 'body'
}
ast::mac_embed_type(ty) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn visit_pat<E>(p: @pat, e: E, v: vt<E>) {
}
pat_ident(path, inner) {
visit_path(path, e, v);
option::may(inner, {|subpat| v.visit_pat(subpat, e, v)});
option::with_option_do(inner, {|subpat| v.visit_pat(subpat, e, v)});
}
pat_lit(ex) { v.visit_expr(ex, e, v); }
pat_range(e1, e2) { v.visit_expr(e1, e, v); v.visit_expr(e2, e, v); }
Expand Down
26 changes: 13 additions & 13 deletions src/rustc/metadata/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,15 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,

#debug["Encoding side tables for id %d", id];

option::may(tcx.def_map.find(id)) {|def|
option::with_option_do(tcx.def_map.find(id)) {|def|
ebml_w.tag(c::tag_table_def) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
ast::serialize_def(ebml_w, def)
}
}
}
option::may((*tcx.node_types).find(id as uint)) {|ty|
option::with_option_do((*tcx.node_types).find(id as uint)) {|ty|
ebml_w.tag(c::tag_table_node_type) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand All @@ -709,7 +709,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
}
}

option::may(tcx.node_type_substs.find(id)) {|tys|
option::with_option_do(tcx.node_type_substs.find(id)) {|tys|
ebml_w.tag(c::tag_table_node_type_subst) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand All @@ -718,7 +718,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
}
}

option::may(tcx.freevars.find(id)) {|fv|
option::with_option_do(tcx.freevars.find(id)) {|fv|
ebml_w.tag(c::tag_table_freevars) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand All @@ -730,7 +730,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
}

let lid = {crate: ast::local_crate, node: id};
option::may(tcx.tcache.find(lid)) {|tpbt|
option::with_option_do(tcx.tcache.find(lid)) {|tpbt|
ebml_w.tag(c::tag_table_tcache) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand All @@ -739,7 +739,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
}
}

option::may(tcx.ty_param_bounds.find(id)) {|pbs|
option::with_option_do(tcx.ty_param_bounds.find(id)) {|pbs|
ebml_w.tag(c::tag_table_param_bounds) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand All @@ -753,7 +753,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
// is what we actually use in trans, all modes will have been
// resolved.
//
//option::may(tcx.inferred_modes.find(id)) {|m|
//option::with_option_do(tcx.inferred_modes.find(id)) {|m|
// ebml_w.tag(c::tag_table_inferred_modes) {||
// ebml_w.id(id);
// ebml_w.tag(c::tag_table_val) {||
Expand All @@ -762,25 +762,25 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
// }
//}

option::may(ccx.maps.mutbl_map.find(id)) {|_m|
option::with_option_do(ccx.maps.mutbl_map.find(id)) {|_m|
ebml_w.tag(c::tag_table_mutbl) {||
ebml_w.id(id);
}
}

option::may(ccx.maps.copy_map.find(id)) {|_m|
option::with_option_do(ccx.maps.copy_map.find(id)) {|_m|
ebml_w.tag(c::tag_table_copy) {||
ebml_w.id(id);
}
}

option::may(ccx.maps.spill_map.find(id)) {|_m|
option::with_option_do(ccx.maps.spill_map.find(id)) {|_m|
ebml_w.tag(c::tag_table_spill) {||
ebml_w.id(id);
}
}

option::may(ccx.maps.last_uses.find(id)) {|m|
option::with_option_do(ccx.maps.last_uses.find(id)) {|m|
ebml_w.tag(c::tag_table_last_use) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand All @@ -792,7 +792,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
// impl_map is not used except when emitting metadata,
// don't need to keep it.

option::may(ccx.maps.method_map.find(id)) {|mo|
option::with_option_do(ccx.maps.method_map.find(id)) {|mo|
ebml_w.tag(c::tag_table_method_map) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand All @@ -801,7 +801,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
}
}

option::may(ccx.maps.vtable_map.find(id)) {|dr|
option::with_option_do(ccx.maps.vtable_map.find(id)) {|dr|
ebml_w.tag(c::tag_table_vtable_map) {||
ebml_w.id(id);
ebml_w.tag(c::tag_table_val) {||
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn get_dep_hashes(cstore: cstore) -> [str] {

fn get_path(cstore: cstore, d: ast::def_id) -> [str] {
// let f = bind str::split_str(_, "::");
option::maybe(p(cstore).mod_path_map.find(d), [],
option::with_option(p(cstore).mod_path_map.find(d), [],
{|ds| str::split_str(ds, "::")})
}
// Local Variables:
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn class_member_id(d: ebml::doc, cdata: cmd) -> ast::def_id {

fn field_mutability(d: ebml::doc) -> ast::class_mutability {
// Use maybe_get_doc in case it's a method
option::maybe(ebml::maybe_get_doc(d, tag_class_mut),
option::with_option(ebml::maybe_get_doc(d, tag_class_mut),
ast::class_immutable,
{|d|
alt ebml::doc_as_u8(d) as char {
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn check_item(it: @item, &&_is_const: bool, v: visit::vt<bool>) {
item_const(_, ex) { v.visit_expr(ex, true, v); }
item_enum(vs, _) {
for var in vs {
option::may(var.node.disr_expr) {|ex|
option::with_option_do(var.node.disr_expr) {|ex|
v.visit_expr(ex, true, v);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/rustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
}
}
expr_path(_) | expr_field(_, _, _) {
option::may(cx.tcx.node_type_substs.find(e.id)) {|ts|
option::with_option_do(cx.tcx.node_type_substs.find(e.id)) {|ts|
let bounds = alt check e.node {
expr_path(_) {
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(e.id));
Expand Down Expand Up @@ -235,7 +235,7 @@ fn check_stmt(stmt: @stmt, cx: ctx, v: visit::vt<ctx>) {
fn check_ty(aty: @ty, cx: ctx, v: visit::vt<ctx>) {
alt aty.node {
ty_path(_, id) {
option::may(cx.tcx.node_type_substs.find(id)) {|ts|
option::with_option_do(cx.tcx.node_type_substs.find(id)) {|ts|
let did = ast_util::def_id_of_def(cx.tcx.def_map.get(id));
let bounds = ty::lookup_item_type(cx.tcx, did).bounds;
vec::iter2(ts, *bounds) {|ty, bound|
Expand Down
6 changes: 3 additions & 3 deletions src/rustc/middle/last_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
clear_in_current(cx, root_id, false);
}
_ {
option::may(def_is_owned_local(cx, my_def)) {|nid|
option::with_option_do(def_is_owned_local(cx, my_def)) {|nid|
clear_in_current(cx, nid, false);
cx.current += [{def: nid,
uses: cons(var_use(ex.id), @nil)}];
Expand Down Expand Up @@ -192,7 +192,7 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
alt ty::arg_mode(cx.tcx, arg_t) {
by_ref | by_val | by_mutbl_ref {
let def = cx.def_map.get(arg.id);
option::may(def_is_owned_local(cx, def)) {|id|
option::with_option_do(def_is_owned_local(cx, def)) {|id|
clear_in_current(cx, id, false);
cx.spill_map.insert(id, ());
}
Expand Down Expand Up @@ -247,7 +247,7 @@ fn visit_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
alt cx.tcx.freevars.find(id) {
some(vars) {
for v in *vars {
option::may(def_is_owned_local(cx, v.def)) {|nid|
option::with_option_do(def_is_owned_local(cx, v.def)) {|nid|
clear_in_current(cx, nid, false);
cx.current += [{def: nid,
uses: cons(close_over(id), @nil)}];
Expand Down
4 changes: 2 additions & 2 deletions src/rustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1936,7 +1936,7 @@ fn check_exports(e: @env) {


fn maybe_add_reexport(e: @env, export_id: node_id, def: option<def>) {
option::may(def) {|def|
option::with_option_do(def) {|def|
add_export(e, export_id, def_id_of_def(def), true);
}
}
Expand Down Expand Up @@ -2118,7 +2118,7 @@ fn find_impls_in_view_item(e: env, vi: @ast::view_item,
ast::view_path_simple(name, pt, id) {
let mut found = [];
if vec::len(*pt) == 1u {
option::may(sc) {|sc|
option::with_option_do(sc) {|sc|
list::iter(sc) {|level|
if vec::len(found) == 0u {
for imp in *level {
Expand Down
6 changes: 3 additions & 3 deletions src/rustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ fn trans_if(cx: block, cond: @ast::expr, thn: ast::blk,
let then_cx = scope_block(bcx, "then");
then_cx.block_span = some(thn.span);
let else_cx = scope_block(bcx, "else");
option::may(els) {|e| else_cx.block_span = some(e.span); }
option::with_option_do(els) {|e| else_cx.block_span = some(e.span); }
CondBr(bcx, cond_val, then_cx.llbb, else_cx.llbb);
let then_bcx = trans_block(then_cx, thn, then_dest);
let then_bcx = trans_block_cleanups(then_bcx, then_cx);
Expand Down Expand Up @@ -2744,7 +2744,7 @@ fn trans_call_inner(in_cx: block, fn_expr_ty: ty::t, ret_ty: ty::t,
Unreachable(bcx);
} else if ret_in_loop {
bcx = with_cond(bcx, Load(bcx, option::get(ret_flag))) {|bcx|
option::may(bcx.fcx.loop_ret) {|lret|
option::with_option_do(bcx.fcx.loop_ret) {|lret|
Store(bcx, C_bool(true), lret.flagptr);
Store(bcx, C_bool(false), bcx.fcx.llretptr);
}
Expand Down Expand Up @@ -3816,7 +3816,7 @@ fn alloc_local(cx: block, local: @ast::local) -> block {
}
let val = alloc_ty(cx, t);
if cx.sess().opts.debuginfo {
option::may(simple_name) {|name|
option::with_option_do(simple_name) {|name|
str::as_c_str(name, {|buf|
llvm::LLVMSetValueName(val, buf)
});
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/trans/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ fn build_closure(bcx0: block,
}
}
}
option::may(include_ret_handle) {|flagptr|
option::with_option_do(include_ret_handle) {|flagptr|
let our_ret = alt bcx.fcx.loop_ret {
some({retptr, _}) { retptr }
none { bcx.fcx.llretptr }
Expand Down
Loading

0 comments on commit 21be137

Please sign in to comment.