Defining setter return value - encapsulation

I just had a problem in one of my projects. Maybe I got the wrong concept about encapsulation.
Encapsulation protects member variables from classes, by defining getters and setters methods, now, i was reading that setters must be void, but in that case, how can I know if the function really set the value passed by argument. For example
void setArea(int a) {
if(a>0)
Area = a;
}
How can I be sure that argument "a" was a correct value, wouldnt be better defining the function like this
bool setArea(int a) {
if(a>0) {
Area = a;
return true;
}
return false;
}
Is that ok? that way i can know if a change really happened.

I think what you're looking for is a guard clause that throws an exception if invalid values are set:
void setArea(int a) {
if (a <= 0) throw new InvalidArgumentException(...);
Area = a;
}
But if you want client code to test for invalid values before setting them, you could have this:
bool isAreaValid(int a) {
return a > 0;
}
void setArea(int a) {
if (!isAreaValid(a)) throw new InvalidArgumentException(...);
Area = a;
}
Clients could be coded like this:
if (obj.isAreaValid(myArea)) {
obj.setArea(myArea);
}
But don't stop there. If the concept of area is important, spring it into existence into its own value object to make your design clearer.

Related

Break out of an anonymous function in kotlin

Created a test case to try and represent what I'm trying to do. I can't figure out how to "stop" continuing to do work inside of an anonymous function. In the example below I would like to break out of the "apple" section if the answer is correct. The code below doesn't compile because it says return#apple instead of return#banana which is the only valid option but I wrote it below to try and explain what I'm trying to achieve and better understand how to go about doing something like this.
class FunctionBreakingTest {
#Test fun stopInMiddleOfLambda() {
var answer = "wrong"
doStuff apple# {
doStuff banana# {
answer = "correct"
if (answer == "correct") {
return#apple
}
answer = "wrong"
}
answer = "wrong"
}
assertEquals("correct", answer)
}
private fun doStuff(foo: () -> Unit) = foo.invoke()
}
You need to make doStuff an inline function: non-local return is only supported for lambdas that are inlined.
private inline fun doStuff(foo: () -> Unit) = foo.invoke()
Then your test case passes.
Not only is return#apple illegal, just plain return is also illegal (because the non-local return requires inlining - See the answer by #hotkey, make doStuff inline and then it works)...
(Note that such non-local returns are supported only for lambda expressions passed to inline functions.)
This section of the Kotlin Documentation covers Returns at labels. Note: using anonymous functions instead of lambdas would allow you to eliminate the labels if you wanted (but, you only get local returns, and you would need to modify your code slightly).
#Test fun stopInMiddleOfLambda() {
var answer = "wrong"
doStuff(fun() {
doStuff(fun() {
answer = "correct"
if (answer == "correct") {
return
}
answer = "wrong"
})
if(answer != "wrong") {
return
}
answer = "wrong"
})
assertEquals("correct", answer)
}
...

How to get the class of a VALUE in Ruby C API

I created some classes with Ruby's C API. I want to create a function whose behavior will change depending on the class of the Ruby object.
I tried to use is_a? from Ruby, however, I don't think it's the good way to do this. I checked "Creating Extension Libraries for Ruby" without success. The only direct way to check classes is with the default types.
I have my class "Klass" already created:
VALUE rb_cKlass = rb_define_class("Klass", rb_cObject);
And how I wanted to check if the class is the good one:
VALUE my_function(VALUE self, VALUE my_argument) {
if(rb_check_class(my_argument), rb_cKlass)) {
// do something if my_argument is an instance of Klass
} else {
return Qnil;
}
}
Is there a way to do this?
I came across this recently, and used the RBASIC_CLASS macro, but was getting segfaults in certain scenarios for some unexplained reason.
After scanning through ruby.h, I found the CLASS_OF macro, which returns the class as VALUE of a given object.
VALUE obj = INT2NUM(10);
VALUE klass = CLASS_OF(obj); // rb_cInteger
Using Ruby 2.5
Every ruby object is internally represented by RObject struct (I will copy the source here for the sake of future readers):
struct RObject {
struct RBasic basic;
union {
struct {
uint32_t numiv;
VALUE *ivptr;
void *iv_index_tbl; /* shortcut for RCLASS_IV_INDEX_TBL(rb_obj_class(obj)) */
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
The very first member, RBasic, defines the class:
struct RBasic {
VALUE flags;
const VALUE klass;
}
To get an access to RBasic metadata of anything, one might use RBASIC macro:
RBASIC(my_argument)
To get the class directly, one might use RBASIC_CLASS macro:
RBASIC_CLASS(my_argument)
If you want to stay close to the is_a? Ruby fashion (i.e. check if any of the ancestors is the expected class), you could directly use the C implementation of is_a?, rb_obj_is_kind_of:
rb_obj_is_kind_of(my_argument, rb_cKlass) // Qtrue OR Qfalse
And since Qfalse == 0, you can just use that method as a condition:
VALUE my_function(VALUE self, VALUE my_argument) {
if(rb_obj_is_kind_of(my_argument, rb_cKlass)) {
// do something if my_argument is an instance of Klass
} else {
return Qnil;
}
}
To find this method, just check Object#is_a? documentation and click to toggle source, you'll see the C implementation if it is a C function (hence this will work for most of the standard lib).

determine whether the template arguments are arrays holding the same unqualified type

I am trying to make a template that determines whether the arguments are arrays holding the same unqualified type.
template sameUnqualArrays(A, B) {
enum bool sameUnqualArrays = isArray!A && isArray!B &&
is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B));
}
unittest {
static assert(sameUnqualArrays!(ubyte[], immutable(ubyte[])));
static assert(!sameUnqualArrays!(ubyte[], immutable(byte[])));
static assert(!sameUnqualArrays!(ubyte[], ubyte));
}
Unfortunately the last assert doesn't pass but gives the error:
/usr/include/dmd/phobos/std/traits.d(6062,9): Error: invalid foreach aggregate cast(ubyte)0u
Is this a bug? How to fix this? Is there an easier way to achieve this?
Hmm, it looks like short circuit evaluation is not working here? It might be a bug, but the workaround is easy enough:
import std.traits;
template sameUnqualArrays(A, B) {
static if (isArray!A && isArray!B)
enum bool sameUnqualArrays =
is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B));
else
enum bool sameUnqualArrays = false;
}
unittest {
static assert(sameUnqualArrays!(ubyte[], immutable(ubyte[])));
static assert(!sameUnqualArrays!(ubyte[], immutable(byte[])));
static assert(!sameUnqualArrays!(ubyte[], ubyte));
}

Kotlin: For-loop must have an iterator method - is this a bug?

I have the following code:
public fun findSomeLikeThis(): ArrayList<T>? {
val result = Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>
if (result == null) return null
return ArrayList(result)
}
If I call this like:
var list : ArrayList<Person>? = p1.findSomeLikeThis()
for (p2 in list) {
p2.delete()
p2.commit()
}
It would give me the error:
For-loop range must have an 'iterator()' method
Am I missing something here?
Your ArrayList is of nullable type. So, you have to resolve this. There are several options:
for (p2 in list.orEmpty()) { ... }
or
list?.let {
for (p2 in it) {
}
}
or you can just return an empty list
public fun findSomeLikeThis(): List<T> //Do you need mutable ArrayList here?
= (Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>)?.toList().orEmpty()
try
for(p2 in 0 until list.count()) {
...
...
}
I also face this problem when I loop on some thing it is not an array.
Example
fun maximum(prices: Array<Int>){
val sortedPrices = prices.sort()
for(price in sortedPrices){ // it will display for-loop range must have iterator here (because `prices.sort` don't return Unit not Array)
}
}
This is different case to this question but hope it help
This can also happen in Android when you read from shared preferences and are getting a (potentially) nullable iterable object back like StringSet. Even when you provide a default, the compiler is not able to determine that the returned value will never actually be null. The only way I've found around this is by asserting that the returned expression is not null using !! operator, like this:
val prefs = PreferenceManager.getDefaultSharedPreferences(appContext)
val searches = prefs.getStringSet("saved_searches", setOf())!!
for (search in searches){
...
}

How do I get class of a Ruby object in C?

I have a function that handles two types: NVector and NMatrix; the former is derived from the latter. This function is basically a specialized copy constructor. I want it to return an object of the same type as that upon which it was called, so, NVector returns NVector, not NMatrix.
static VALUE nm_init_modifiedcopy(VALUE self) {
// ... some code ...
// formerly, I had cNMatrix where klass is. But it could also be cNVector!
return Data_Wrap_Struct(klass, mark_func, delete_func, unwrapped_self_copy);
}
How do I get the class property of an object to pass into Data_Wrap_Struct?
Like clockwork, as soon as I ask a question on Stackoverflow, I find the answer.
The macro is CLASS_OF.
static VALUE nm_init_modifiedcopy(VALUE self) {
// ... some code ...
return Data_Wrap_Struct(CLASS_OF(self), mark_func, delete_func, unwrapped_self_copy);
}

Resources