Beginner’s question: If I have a function that ret...
# arrow
s
Beginner’s question: If I have a function that returns a
boolean
. Should I wrap the return type with
Either
or what will be a better return type?
m
Imho: If the function is pure (no side effects, same return value for some input always), there is no need to wrap the
boolean
. Just leave it as it is.
s
Yeah it make sense. I am looking for an article which can summarise all the data types available and it’s usage in scenarios. In other words, a kind of cheat sheet.
m
https://arrow-kt.io/docs/datatypes/intro/ is already pretty comprehensive.
💯 1
r
You can argue that in many cases Boolean is used as a lack of a better defined union type that is more explicit as to what you are modeling
For example in Either fold is preferred over isLeft or isRight because those checks usually mean implicit semantics in your function that you understand by context but are not as explicit as dealing with Left or Right over a fold.
s
Let’s take the following example which is very similar to my usecase:
Copy code
fun isSpoofingEnabled(): Boolean {
    return featureToggle.isEnabled(FEATURE_SPOOFING)
}

fun getModel(): Single<LocationModel> {
    return if(isSpoofingEnabled()){
        Single.just(SpoofedModel())
    } else {
     	Single.just(OriginalModel())   
    }
}
Correct me if I am wrong here, what I have understood so far about FP is a pure function should not have conditional statements. So I was looking for a wrapper which could provide me something like
map
or
flatmap
to return a different type based on the boolean value.
k
A pure function can have conditional statements. A pure function is just one that produces the exact same result every time it’s called with the same parameters. This means no IO, no mutable parameters, no non-local variables.
Copy code
fun squareRoot(x: Int): Option<Int> {
  if(x>=0) return Some(Math.sqrt(x))
  else return None
}
is a perfectly pure function.
FWIW your first function,
isSpoofingEnabled
, is not pure. This is because it appears to be accessing a non-local variable,
featureToggle
That is to say, if in your main function you had:
Copy code
featureToggle.isEnabled = { true }
isSpoofingEnabled() // returns true
featureToggle.isEnabled = { false }
isSpoofingEnabled() // returns false
Same
isSpoofingEnabled
called with same (non-existent) parameters, but different result. This means it’s not pure. Any function with no parameters passed in, or that returns Unit, is most likely going to be non-pure, because if it were pure, it couldn’t do anything.
r
If featureToggle produces no effects we also consider that pure even if it's on the outter scope
k
By “if it produces no effects” do you mean inter alia that it’s immutable? I guess that makes sense. I guess since it’s called
featureToggle
I assumed
isEnabled
is a mutable boolean, but I guess it could be immutable and I was just assuming.
r
yeah, I ment immutable values in the outer scope, but yeah, the name does not help make the point xD.
s
featureToggle
basically will check if Firebase remote config is enabled, then it will feature value from there or it will get value from local store.
r
Then sounds like it's an effect and should be managed in IO.
s
That’s logical. Now I actually got my answer. Thanks @raulraja
👍 1