Rename some core::option functions

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
This commit is contained in:
Tim Chevalier 2012-04-02 15:34:49 -07:00
parent 987bc23629
commit 21be1379d5
31 changed files with 67 additions and 64 deletions

View File

@ -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

View File

@ -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];
}
};

View File

@ -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)
}
}

View File

@ -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)) {
#[doc = "Performs an operation on the contained value or does nothing"];
alt opt { none { } some(t) { f(t); } }
@ -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

View File

@ -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]
@ -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

View File

@ -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); }
}
}
}

View File

@ -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);
}

View File

@ -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));

View File

@ -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 }

View File

@ -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] = [];

View File

@ -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) {

View File

@ -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); }

View File

@ -692,7 +692,7 @@ 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) {||
@ -700,7 +700,7 @@ fn encode_side_tables_for_id(ecx: @e::encode_ctxt,
}
}
}
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) {||
@ -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) {||
@ -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) {||
@ -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) {||
@ -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) {||
@ -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) {||
@ -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) {||
@ -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) {||
@ -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) {||

View File

@ -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:

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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));
@ -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|

View File

@ -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)}];
@ -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, ());
}
@ -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)}];

View File

@ -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);
}
}
@ -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 {

View File

@ -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);
@ -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);
}
@ -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)
});

View File

@ -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 }

View File

@ -842,7 +842,7 @@ fn hash_mono_id(&&mi: mono_id) -> uint {
h = h * alt param {
mono_precise(ty, vts) {
let mut h = ty::type_id(ty);
option::may(vts) {|vts|
option::with_option_do(vts) {|vts|
for vec::each(vts) {|vt| h += hash_mono_id(vt); }
}
h

View File

@ -51,7 +51,7 @@ fn traverse_exports(cx: ctx, vis: [@view_item]) -> bool {
}
fn traverse_export(cx: ctx, exp_id: node_id) {
option::may(cx.exp_map.find(exp_id)) {|defs|
option::with_option_do(cx.exp_map.find(exp_id)) {|defs|
for vec::each(defs) {|def| traverse_def_id(cx, def.id); }
}
}

View File

@ -146,7 +146,7 @@ fn mark_for_expr(cx: ctx, e: @expr) {
}
}
expr_path(_) {
option::may(cx.ccx.tcx.node_type_substs.find(e.id)) {|ts|
option::with_option_do(cx.ccx.tcx.node_type_substs.find(e.id)) {|ts|
let id = ast_util::def_id_of_def(cx.ccx.tcx.def_map.get(e.id));
vec::iter2(type_uses_for(cx.ccx, id, ts.len()), ts) {|uses, subst|
type_needs(cx, uses, subst)
@ -215,7 +215,7 @@ fn handle_body(cx: ctx, body: blk) {
},
visit_block: {|b, cx, v|
visit::visit_block(b, cx, v);
option::may(b.node.expr) {|e|
option::with_option_do(b.node.expr) {|e|
node_type_needs(cx, use_repr, e.id);
}
},

View File

@ -336,7 +336,7 @@ fn find_pre_post_expr(fcx: fn_ctxt, e: @expr) {
let use_cap_item = fn@(&&cap_item: @capture_item) {
let d = local_node_id_to_local_def_id(fcx, cap_item.id);
option::may(d, { |id| use_var(fcx, id) });
option::with_option_do(d, { |id| use_var(fcx, id) });
};
vec::iter(cap_clause.copies, use_cap_item);
vec::iter(cap_clause.moves, use_cap_item);

View File

@ -410,7 +410,7 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
let base_pres = alt vec::last_opt(exs) { none { pres }
some(f) { expr_poststate(fcx.ccx, f) }};
option::may(maybe_base, {|base|
option::with_option_do(maybe_base, {|base|
changed |= find_pre_post_state_expr(fcx, base_pres, base) |
set_poststate_ann(fcx.ccx, e.id,
expr_poststate(fcx.ccx, base))});
@ -611,7 +611,7 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
handle_fail(fcx, pres, post);
ret set_prestate_ann(fcx.ccx, e.id, pres) |
set_poststate_ann(fcx.ccx, e.id, post) |
option::maybe(maybe_fail_val, false, {|fail_val|
option::with_option(maybe_fail_val, false, {|fail_val|
find_pre_post_state_expr(fcx, pres, fail_val)});
}
expr_check(_, p) {

View File

@ -385,7 +385,7 @@ fn mk_ctxt(s: session::session, dm: resolve::def_map, amap: ast_map::map,
region_map: @middle::region::region_map) -> ctxt {
let interner = map::hashmap({|&&k: intern_key|
hash_type_structure(k.struct) +
option::maybe(k.o_def_id, 0u, ast_util::hash_def_id)
option::with_option(k.o_def_id, 0u, ast_util::hash_def_id)
}, {|&&a, &&b| a == b});
@{interner: interner,
mut next_id: 0u,

View File

@ -3170,7 +3170,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
fcx.write_ty(id, typ);
}
ast::expr_rec(fields, base) {
option::may(base) {|b| check_expr(fcx, b); }
option::with_option_do(base) {|b| check_expr(fcx, b); }
let fields_t = vec::map(fields, {|f|
bot |= check_expr(fcx, f.node.expr);
let expr_t = fcx.expr_ty(f.node.expr);

View File

@ -50,7 +50,7 @@ fn fold_crate(
{
topmod: {
item: {
name: option::from_maybe(attrs.name, doc.topmod.name())
name: option::get_or_default(attrs.name, doc.topmod.name())
with doc.topmod.item
}
with doc.topmod

View File

@ -125,14 +125,15 @@ fn config_from_opts(
let result = result::chain(result) {|config|
let output_dir = getopts::opt_maybe_str(match, opt_output_dir());
result::ok({
output_dir: option::from_maybe(output_dir, config.output_dir)
output_dir: option::get_or_default(output_dir, config.output_dir)
with config
})
};
let result = result::chain(result) {|config|
let output_format = getopts::opt_maybe_str(
match, opt_output_format());
option::maybe(output_format, result::ok(config)) {|output_format|
option::with_option(output_format, result::ok(config))
{|output_format|
result::chain(parse_output_format(output_format)) {|output_format|
result::ok({
output_format: output_format
@ -143,7 +144,8 @@ fn config_from_opts(
};
let result = result::chain(result) {|config|
let output_style = getopts::opt_maybe_str(match, opt_output_style());
option::maybe(output_style, result::ok(config)) {|output_style|
option::with_option(output_style, result::ok(config))
{|output_style|
result::chain(parse_output_style(output_style)) {|output_style|
result::ok({
output_style: output_style

View File

@ -195,7 +195,7 @@ fn build_reexport_path_map(srv: astsrv::srv, -def_map: def_map) -> path_map {
}
if reexportdocs.len() > 0u {
option::may(path_map.find(modpath)) {|docs|
option::with_option_do(path_map.find(modpath)) {|docs|
reexportdocs = docs + vec::filter(reexportdocs, {|x|
!vec::contains(docs, x)
});