Does that look like a useful addition to the language (or a macro), or what do you think?
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Being able to use an Option as a Boolean feels a little too much, but maybe something like:
implicit class BoolOption(val o: Option[_]) extends AnyVal {
def apply(): Boolean = o.isDefined
}
if (!foo()) println("oops, foo is not defined")
Although foreach, or pattern matching, usually seems enough.
--
--
2) you lose all the information pertaining to your data.
On Tue, Aug 20, 2013 at 11:33 AM, Vlad Patryshev <vpatr...@gmail.com> wrote:
2) you lose all the information pertaining to your data.I've come to see Boolean (and Int, etc.) as the moral equivalent of null. Bit patterns disassociated from meaning. It's barely an improvement on not using types at all. My usual example in this domain is Ordering.
trait Ordering[T] { def compare(x: T, y: T): Int }We are defining a relation which admits three outcomes: LT, EQ, GT. So let's see, how to encode that. Let's choose a data type with 4294967296 states and say that by convention the first 2147483648 states mean LT, the next state means EQ, and the last 2147483647 states mean GT.And THEN let's use the raw 32-bit pattern as the actual type, so that the values cannot be distinguished from any other 32-bit pattern. 5 monkeys, 5 trips to Cleveland, and GT, they're all completely interchangeable and indistinguishable.If anyone proposed this in another context they'd be laughed out of the room. But we put up with this and worse, and there seems little demand to improve upon it, even though we have the power to do it without performance cost, or at least very nearly so. It makes me wonder if I should have hitched the trailer onto javascript.
+1 to this. paulp detailed what's wrong with using booleans as an output type in your API, but using them as an input type is just as silly.
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
It's more code, but it's code that is easier to maintain.
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Also, when considering this question we should strongly distinguish between abstraction and implementation. scala inflicts a ton of boilerplate on you to define a 2 or 3 state enum, but that tells us nothing about the wisdom of using more meaningful types than "Boolean".
I've come to see Boolean (and Int, etc.) as the moral equivalent of null.
I can offer that one also, enclosed below: https://groups.google.com/forum/#!msg/scala-internals/5VhsT3PhcSg/MWuqSJieVeYJOn Thu, Aug 22, 2013 at 7:47 AM, Haoyi Li <haoy...@gmail.com> wrote:
+1 to this. paulp detailed what's wrong with using booleans as an output type in your API, but using them as an input type is just as silly.
Usingbooleans to partition behavior among a small set of discretealternatives means you forego using one of the best features ofobjects - polymorphic dispatch - in order to drizzle your code withmeaningless one-bit carriers. Here's a sample type signature, whatdoes this tell us?(Type, Boolean, Boolean) => Type
When you use polymorphism, you don't have to start tacking thoseBooleans on as meaningless appendages when you realize you need accessto b in method g. Nor do you find yourself with "invalid states" whenyou have three states to cover, and you need the booleans to arrivetrue/true, true/false, and false/true, but false/false means nothing.
When you use polymorphism, you already encapsulated data and behavior.You can extend.I do recall a before/after where I expunged boolean parameters.Samples of the "before" code:- case IDENTIFIER | BACKQUOTED_IDENT | THIS | SUPER =>- path(true, false)...- t = (t /: annotations(false, false)) (makeAnnotated)...- params += typeParam(NoMods.withAnnotations(annotations(true, false)))Now we can say they should have used named parameters on those truesand falses until we're blue in the face (even if they didn't existyet, doesn't matter) and it won't make the names appear.
I don't care to discuss the boilerplate requirements in scala or other languages. I know this can be expressed by the programmer and enforced in a type system without it being some kind of redundancy tragedy. We know how to reuse behavior. We know how to distinguish different things when there is value in doing so. All we have to do is do it.
--
--
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I would also strongly argue that trying to find the perfect type for every argument or return value will lead to an enormous overhead of type declarations but even more so type conversions (especially from XinLibraryA to XinLibraryB) and will lead to code such as:Hello,
if(statusOfX.to[CanBePerformedSafely].to[Boolean]) doXThere will be development cycles that go like this:(1) Slightly different types will be introduced(2) We need to convert between these slightly different types
(3) Conversion needs to be consistent(4) The slightly different types will become more and more the same
I am very wary of introducing something called "units". The name suggests inspiration from units of measurement. Formalizing units of measurement is an endeavor that seems deceptively simple and turns out extremely hard.
On Thu, Aug 22, 2013 at 10:43 AM, Oliver Ruebenacker <cur...@gmail.com> wrote:
Why no one made a suggestion on how to re-write:Hello,
def todayIsWorkDay : Boolean = ! (todayIsSunday | todayIsHoliday)
It's kind of a bad example, because “work day” is not a boolean property. There are half work days also.
With that said, I think Boolean parameters are worse than Boolean return values. I don't think anyone seriously would want all the Boolean return values on the collections library to be enums or the like, e.g. contains
, isEmpty
, etc.
On Thu, Aug 22, 2013 at 12:01 PM, Oliver Ruebenacker <cur...@gmail.com> wrote:
>
> Hello,
>
> So, instead of
>
> if(todayIsWorkDay) wakeMeUp
>
> I would then have to use something with isInstanceOf or match?
If there are only two cases, you can use ==. -Lex