Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Optional instead of nullptr for some method parameters #2615

Merged
merged 18 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ext/tm/include/tm/optional.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <assert.h>
#include <functional>
#include <stdio.h>
#include <utility>

Expand Down Expand Up @@ -212,6 +213,31 @@ class Optional {
return fallback;
}

/**
* Returns a copy of the underlying value or
* the result of calling the fallback lambda.
*
* ```
* auto obj = Thing(1);
* auto opt = Optional<Thing>(obj);
* assert_eq(Thing(1), opt.value_or([]() { return Thing(2); }));
* ```
*
* If we don't have a value present, then the fallback
* is called and the result returned.
*
* ```
* auto opt = Optional<Thing>();
* assert_eq(Thing(2), opt.value_or([]() { return Thing(2); }));
* ```
*/
T value_or(std::function<T()> fallback) const {
if (present())
return value();
else
return fallback();
}

/**
* Returns a reference to the underlying value.
*
Expand Down
42 changes: 21 additions & 21 deletions include/natalie/array_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ArrayObject : public Object {

bool is_empty() { return m_vector.is_empty(); }

Value initialize(Env *, Value, Value, Block *);
Value initialize(Env *, Optional<Value>, Optional<Value>, Block *);

Value add(Env *, Value);
Value any(Env *, Args &&, Block *);
Expand All @@ -135,7 +135,7 @@ class ArrayObject : public Object {
Value compact(Env *);
Value compact_in_place(Env *);
Value concat(Env *, Args &&);
Value cycle(Env *, Value, Block *);
Value cycle(Env *, Optional<Value>, Block *);
Value delete_at(Env *, Value);
Value delete_if(Env *, Block *);
Value delete_item(Env *, Value, Block *);
Expand All @@ -147,53 +147,53 @@ class ArrayObject : public Object {
Value each_index(Env *, Block *);
bool eq(Env *, Value);
bool eql(Env *, Value);
Value fetch(Env *, Value, Value, Block *);
Value fill(Env *, Value, Value, Value, Block *);
Value first(Env *, Value);
Value flatten(Env *, Value);
Value flatten_in_place(Env *, Value);
Value fetch(Env *, Value, Optional<Value>, Block *);
Value fill(Env *, Optional<Value>, Optional<Value>, Optional<Value>, Block *);
Value first(Env *, Optional<Value>);
Value flatten(Env *, Optional<Value>);
Value flatten_in_place(Env *, Optional<Value>);
Value hash(Env *);
bool include(Env *, Value);
Value index(Env *, Value, Block *);
Value index(Env *, Optional<Value>, Block *);
Value initialize_copy(Env *, Value);
Value inspect(Env *);
Value insert(Env *, Args &&);
Value intersection(Env *, Value);
Value intersection(Env *, Args &&);
bool intersects(Env *, Value);
Value _subjoin(Env *, Value, Value);
Value join(Env *, Value);
Value join(Env *, Optional<Value>);
Value keep_if(Env *, Block *);
Value last(Env *, Value);
Value last(Env *, Optional<Value>);
Value ltlt(Env *, Value);
Value map(Env *, Block *);
Value map_in_place(Env *, Block *);
Value max(Env *, Value, Block *);
Value min(Env *, Value, Block *);
Value max(Env *, Optional<Value>, Block *);
Value min(Env *, Optional<Value>, Block *);
Value minmax(Env *, Block *);
Value multiply(Env *, Value);
Value none(Env *, Args &&, Block *);
Value one(Env *, Args &&, Block *);
Value pack(Env *, Value, Value);
Value pop(Env *, Value);
Value pop(Env *, Optional<Value>);
Value product(Env *, Args &&, Block *);
Value push(Env *, Args &&);
Value rassoc(Env *, Value);
Value ref(Env *, Value, Value = nullptr);
Value refeq(Env *, Value, Value, Value);
Value ref(Env *, Value, Optional<Value> = {});
Value refeq(Env *, Value, Value, Optional<Value> = {});
Value reject(Env *, Block *);
Value reject_in_place(Env *, Block *);
Value reverse(Env *);
Value reverse_each(Env *, Block *);
Value reverse_in_place(Env *);
Value rindex(Env *, Value, Block *);
Value rotate(Env *, Value);
Value rotate_in_place(Env *, Value);
Value rindex(Env *, Optional<Value>, Block *);
Value rotate(Env *, Optional<Value>);
Value rotate_in_place(Env *, Optional<Value>);
Value select(Env *, Block *);
Value select_in_place(Env *, Block *);
bool select_in_place(std::function<bool(Value &)>);
Value shift(Env *, Value);
Value slice_in_place(Env *, Value, Value);
Value shift(Env *, Optional<Value>);
Value slice_in_place(Env *, Value, Optional<Value>);
Value sort(Env *, Block *);
Value sub(Env *, Value);
static Value try_convert(Env *, Value);
Expand Down Expand Up @@ -227,7 +227,7 @@ class ArrayObject : public Object {
nat_int_t _resolve_index(nat_int_t) const;
bool _flatten_in_place(Env *, nat_int_t depth, Hashmap<ArrayObject *> visited_arrays = Hashmap<ArrayObject *> {});
Value _slice_in_place(nat_int_t start, nat_int_t end, bool exclude_end);
Value find_index(Env *, Value, Block *, bool = false);
Value find_index(Env *, Optional<Value>, Block *, bool = false);
bool include_eql(Env *, Value);
};

Expand Down
2 changes: 1 addition & 1 deletion include/natalie/class_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ClassObject : public ModuleObject {

Type object_type() { return m_object_type; }

Value initialize(Env *, Value, Block *);
Value initialize(Env *, Optional<Value>, Block *);

bool is_singleton() const { return m_is_singleton; }
void set_is_singleton(bool is_singleton) { m_is_singleton = is_singleton; }
Expand Down
8 changes: 4 additions & 4 deletions include/natalie/float_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,26 @@ class FloatObject : public Object {
Value abs(Env *) const;
Value add(Env *, Value);
Value arg(Env *);
Value ceil(Env *, Value);
Value ceil(Env *, Optional<Value>);
Value cmp(Env *, Value);
Value coerce(Env *, Value);
Value denominator(Env *) const;
Value div(Env *, Value);
Value divmod(Env *, Value);
Value floor(Env *, Value);
Value floor(Env *, Optional<Value>);
Value mod(Env *, Value);
Value mul(Env *, Value);
Value numerator(Env *) const;
Value next_float(Env *) const;
Value pow(Env *, Value);
Value prev_float(Env *) const;
Value round(Env *, Value);
Value round(Env *, Optional<Value>);
Value sub(Env *, Value);
Value to_f() const { return new FloatObject { *this }; }
Value to_i(Env *) const;
Value to_r(Env *) const;
Value to_s() const;
Value truncate(Env *, Value);
Value truncate(Env *, Optional<Value>);

bool lt(Env *, Value);
bool lte(Env *, Value);
Expand Down
6 changes: 3 additions & 3 deletions include/natalie/hash_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class HashObject : public Object {
bool is_empty() { return m_hashmap.size() == 0; }

Value get(Env *, Value);
Value get_default(Env *, Value = nullptr);
Value get_default(Env *, Optional<Value> = {});
Value set_default(Env *, Value);

void put(Env *, Value, Value);
Expand Down Expand Up @@ -194,12 +194,12 @@ class HashObject : public Object {
bool lte(Env *, Value);
bool lt(Env *, Value);
Value except(Env *, Args &&);
Value fetch(Env *, Value, Value, Block *);
Value fetch(Env *, Value, Optional<Value> = {}, Block * = nullptr);
Value fetch_values(Env *, Args &&, Block *);
Value hash(Env *);
bool has_key(Env *, Value);
bool has_value(Env *, Value);
Value initialize(Env *, Value, Value = nullptr, Block * = nullptr);
Value initialize(Env *, Optional<Value>, Optional<Value> = {}, Block * = nullptr);
Value inspect(Env *);
Value keep_if(Env *, Block *);
Value keys(Env *);
Expand Down
16 changes: 8 additions & 8 deletions include/natalie/integer_methods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class IntegerMethods {

static String to_s(const Integer self) { return self.to_string(); }

static Value to_s(Env *, Integer self, Value = nullptr);
static Value to_s(Env *, Integer self, Optional<Value> = {});
static Value to_i(Integer self) { return self; }
static Value to_f(Integer self);
static Value add(Env *, Integer, Value);
Expand All @@ -73,7 +73,7 @@ class IntegerMethods {
static Value pow(Env *, Integer, Integer);
static Value pow(Env *, Integer, Value);
static Value powmod(Env *, Integer, Integer, Integer);
static Value powmod(Env *, Integer, Value, Value);
static Value powmod(Env *, Integer, Value, Optional<Value>);
static Value cmp(Env *, Integer, Value);
static Value times(Env *, Integer, Block *);
static Value bitwise_and(Env *, Integer, Value);
Expand All @@ -85,20 +85,20 @@ class IntegerMethods {
static Value pred(Env *env, Integer self) { return self - 1; }
static Value size(Env *, Integer);
static Value succ(Env *, Integer self) { return self + 1; }
static Value ceil(Env *, Integer, Value);
static Value ceil(Env *, Integer, Optional<Value>);
static Value coerce(Env *, Value, Value);
static Value floor(Env *, Integer, Value);
static Value floor(Env *, Integer, Optional<Value>);
static Value gcd(Env *, Integer, Value);
static Value abs(Env *, Integer self) { return self.is_negative() ? -self : self; }
static Value chr(Env *, Integer, Value);
static Value chr(Env *, Integer, Optional<Value> = {});
static Value negate(Env *, Integer self) { return -self; }
static Value numerator(Integer self) { return self; }
static Value complement(Env *, Integer self) { return ~self; }
static Value ord(Integer self) { return self; }
static Value denominator() { return Value::integer(1); }
static Value round(Env *, Integer, Value, Value);
static Value truncate(Env *, Integer, Value);
static Value ref(Env *, Integer, Value, Value);
static Value round(Env *, Integer, Optional<Value>, Optional<Value>);
static Value truncate(Env *, Integer, Optional<Value>);
static Value ref(Env *, Integer, Value, Optional<Value>);

static bool neq(Env *env, Value self, Value other) { return self.send(env, "=="_s, { other }).is_falsey(); }
static bool eq(Env *, Integer, Value);
Expand Down
8 changes: 4 additions & 4 deletions include/natalie/kernel_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ class KernelModule {
static bool is_frozen(Value self) { return self.is_frozen(); }
static Value loop(Env *env, Value self, Block *block);
static Value method(Env *env, Value self, Value name);
static Value methods(Env *env, Value self, Value regular_val);
static Value methods(Env *env, Value self, Optional<Value> regular_val);
static bool neqtilde(Env *env, Value self, Value other);
static Value private_methods(Env *env, Value self, Value recur = nullptr);
static Value protected_methods(Env *env, Value self, Value recur = nullptr);
static Value public_methods(Env *env, Value self, Value recur = nullptr);
static Value private_methods(Env *env, Value self, Optional<Value> recur = {});
static Value protected_methods(Env *env, Value self, Optional<Value> recur = {});
static Value public_methods(Env *env, Value self, Optional<Value> recur = {});
static Value remove_instance_variable(Env *env, Value self, Value name_val);
static bool respond_to_missing(Env *, Value, Value, Value) { return false; }
static bool respond_to_method(Env *, Value, Value, Value);
Expand Down
20 changes: 10 additions & 10 deletions include/natalie/module_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ class ModuleObject : public Object {
Value const_find(Env *, SymbolObject *, ConstLookupSearchMode = ConstLookupSearchMode::Strict, ConstLookupFailureMode = ConstLookupFailureMode::ConstMissing);

Value const_get(SymbolObject *) const;
Value const_get(Env *, Value, Value = nullptr);
Value const_get(Env *, Value, Optional<Value> = {});
Value const_fetch(SymbolObject *) const;
Value const_set(SymbolObject *, Value);
Value const_set(SymbolObject *, MethodFnPtr, StringObject *);
Value const_set(Env *, Value, Value);

void remove_const(SymbolObject *);
Value remove_const(Env *, Value);
Value constants(Env *, Value) const;
Value constants(Env *, Optional<Value>) const;
Value const_missing(Env *, Value);

void make_method_alias(Env *, SymbolObject *, SymbolObject *);
Expand Down Expand Up @@ -101,10 +101,10 @@ class ModuleObject : public Object {
bool class_variable_defined(Env *, Value);
Value class_variable_get(Env *, Value);
Value class_variable_set(Env *, Value, Value);
ArrayObject *class_variables(Value = nullptr) const;
ArrayObject *class_variables(Optional<Value> = {}) const;
Value remove_class_variable(Env *, Value);

Value define_method(Env *, Value, Value, Block *);
Value define_method(Env *, Value, Optional<Value>, Block *);
SymbolObject *define_method(Env *, SymbolObject *, MethodFnPtr, int);
SymbolObject *define_method(Env *, SymbolObject *, Block *);
SymbolObject *undefine_method(Env *, SymbolObject *);
Expand All @@ -118,11 +118,11 @@ class ModuleObject : public Object {
Value instance_method(Env *, Value);
Value public_instance_method(Env *, Value);

Value instance_methods(Env *, Value, std::function<bool(MethodVisibility)>);
Value instance_methods(Env *, Value);
Value private_instance_methods(Env *, Value);
Value protected_instance_methods(Env *, Value);
Value public_instance_methods(Env *, Value);
Value instance_methods(Env *, Optional<Value>, std::function<bool(MethodVisibility)>);
Value instance_methods(Env *, Optional<Value>);
Value private_instance_methods(Env *, Optional<Value>);
Value protected_instance_methods(Env *, Optional<Value>);
Value public_instance_methods(Env *, Optional<Value>);

ArrayObject *ancestors(Env *);
bool ancestors_includes(Env *, ModuleObject *);
Expand Down Expand Up @@ -163,7 +163,7 @@ class ModuleObject : public Object {
Value private_constant(Env *, Args &&);
Value public_constant(Env *, Args &&);

bool const_defined(Env *, Value, Value = nullptr);
bool const_defined(Env *, Value, Optional<Value> = {});
Value alias_method(Env *, Value, Value);
Value remove_method(Env *, Args &&);
Value undef_method(Env *, Args &&);
Expand Down
3 changes: 2 additions & 1 deletion include/natalie/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "natalie/object_type.hpp"
#include "natalie/value.hpp"
#include "tm/hashmap.hpp"
#include "tm/optional.hpp"

namespace Natalie {

Expand Down Expand Up @@ -122,7 +123,7 @@ class Object : public Cell {
static SymbolObject *define_singleton_method(Env *, Value, SymbolObject *, Block *);
static SymbolObject *undefine_singleton_method(Env *, Value, SymbolObject *);

Value main_obj_define_method(Env *, Value, Value, Block *);
Value main_obj_define_method(Env *, Value, Optional<Value>, Block *);
Value main_obj_inspect(Env *);

virtual Value private_method(Env *, Args &&);
Expand Down
9 changes: 4 additions & 5 deletions include/natalie/random_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class RandomObject : public Object {

~RandomObject() { delete m_generator; }

Value initialize(Env *, Value);
Value initialize(Env *, Optional<Value> = {});

Value bytes(Env *, Value);
Value rand(Env *, Value);
Value rand(Env *, Optional<Value>);
Value seed() const { return Value::integer(m_seed); }

virtual void gc_inspect(char *buf, size_t len) const override {
Expand All @@ -42,9 +42,8 @@ class RandomObject : public Object {
return Value::integer(std::random_device()());
}

static Value srand(Env *env, Value seed) {
if (!seed)
seed = new_seed(env);
static Value srand(Env *env, Optional<Value> seed_arg) {
auto seed = seed_arg.value_or([&env]() { return new_seed(env); });
auto default_random = GlobalEnv::the()->Random()->const_fetch("DEFAULT"_s).as_random();
auto old_seed = default_random->seed();
auto new_seed = IntegerMethods::convert_to_native_type<nat_int_t>(env, seed);
Expand Down
8 changes: 4 additions & 4 deletions include/natalie/range_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ class RangeObject : public Object {
Value end() { return m_end; }
bool exclude_end() const { return m_exclude_end; }

Value initialize(Env *, Value, Value, Value);
Value initialize(Env *, Value, Value, Optional<Value>);
Value to_a(Env *);
Value each(Env *, Block *);
Value first(Env *, Value);
Value first(Env *, Optional<Value>);
Value inspect(Env *);
Value last(Env *, Value);
Value last(Env *, Optional<Value>);
String to_s() const;
Value to_s(Env *);
bool eq(Env *, Value);
bool eql(Env *, Value);
bool include(Env *, Value);
Value bsearch(Env *, Block *);
Value step(Env *, Value, Block *);
Value step(Env *, Optional<Value>, Block *);

static Value size_fn(Env *env, Value self, Args &&, Block *) {
return Value::integer(self.as_range()->to_a(env).as_array()->size());
Expand Down
Loading
Loading