/Jesper Nordenberg
--
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-language+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
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.
I think this is worth considering to make scala.Option a value class
unless some major problems are discovered.
The other advantage to adding a new type is that there is currently
code in the wild that depends on being able to create Some(null). :/
(At least, last time this came up that was considered a big stumbling
block I think.)
Well, obviosuly Some(null) == None. Probably some people rely on Some(null) != None (which is sorta weird IMHO as None is meant to replace null) which would be a major problem.
If we add support for postfix type syntax we can
steal Kotlin's nice syntax for this nullable type: String?
That would be horrible.
Yes, that would be a major problem. A better strategy might be to add a new option type (OptionVal?)
--
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-language+unsubscribe@googlegroups.com.
If this was a democracy, and I had a vote, I'd vote for Opt.
Paul Phillips skrev 2013-03-13 01:37:Yes, maybe it's better to create a new value class that only targets the concept of nullability and provide reasonable conversion methods between Option and this type.
The really big stumbling block is that Option is entrenched as a means
of communicating "maybe there is a result" but also a means of
communicating "it is not null", and these are not the same thing,
neither interchangeable nor unifiable in a space of one bit.
If we add support for postfix type syntax we can steal Kotlin's nice syntax for this nullable type: String?
/Jesper Nordenberg
--
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-language+unsubscribe@googlegroups.com.
On Tue, Mar 12, 2013 at 5:52 PM, Jesper Nordenberg <mega...@yahoo.com> wrote:Yes, that would be a major problem. A better strategy might be to add a new option type (OptionVal?)
If this was a democracy, and I had a vote, I'd vote for Opt.
In my opinion it's not worth it to have two different types which do nearly the same thing. Not only will the transition be horrible but it would mean adding additional conversion methods to tons of existing classes.
Of course it would be nice to have that optimization, but sometimes one has to make compromises.
--
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.
If this was a democracy, and I had a vote, I'd vote for Opt.
+1, this would be great -- even more so if with a refactoring tool to find/replace Option. The status quo with is (imho) embarrassing -- this change needs to happen. So we might as well do it sooner than later. And as for the name, I even prefer Opt to Option :D
It does not follow that lightweight options should be abandoned, just that they might not be a universal panacea.
--
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.
BTW - random thoughts from the past ->
1. It is the meaning of null that developers are confused about. Some take it to mean "exists" or "not exists" and so it conflates our option opinions (is it a way to stop using null? Well it should be for the exists/not-exists use case)
2. We had thought at one time about the following:
trait Option[+T] extends Any
trait OptVal[+T <: NotNull] extends Option [T]
case class SomeVal[T](value: T) extends AnyVal with OptVal[T]
case object SomeNull extends Option[Null]
case object None extends Option[Nothing]
object Some // extractor and unapply
Which I think still fails to give the appropriate benefits and adds a fun level of confusion. Did we ever dig into this option further?
--
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.
All this stuff sounds pretty much like a job for the runtime. I'm not willing to further complicate/uglify the Option/Either/Try/Validation space, just because HotSpot people can't get anything done.
I've done some testing with an Option value class and it seems to work well. In most use cases I think this would save quite a bit of memory and a lot of object allocations. In the, I assume, less common case of up-casting None to a super type it will consume more memory than the current Option type and entail boxing. Here's my short blog post about it:
http://jnordenberg.blogspot.se/2013/03/a-more-efficient-option.html
I think this is worth considering to make scala.Option a value class unless some major problems are discovered.
/Jesper Nordenberg
--
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-language+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.
Hi,Hey, nice one :-p I think you can improve performance a bit if you define noneValue as a private object rather than a private val. Being a top-level module (object), the code to load it is much shorter (it's a just a Load-Static-Field or something).
I will have a look at the byte code when I have some time. What is the best way to look at the byte code of a value class? The whole point of a value class is that it kind of vanishes at runtime if used properly. So use it in a method of another class and watch the generated code?
final class Option[+A](val value: A) extends AnyVal abstract class Foo[A] { def f(): Option[A] } class Bar[A] extends Foo[A] { def f(): Option[A] = ??? }
a.scala:7: error: bridge generated for member method f: ()Option[A] in class Bar which overrides method f: ()Option[A] in class Foo clashes with definition of the member itself; both have erased type ()Object class Bar[A] extends Foo[A] { def f(): Option[A] = ??? } ^ one error found
Hmm, I'm curious. Why would instanceOf be faster than eq?
In the end I would expect both to end up being no more than a pointer comparison. If not I'd say there's a bug somewhere.
If so, and indeed, one pointer comparison is faster than the other, that must then be due to memory locality no? Is it more likely for the pointer to a class to reside in the cpu cache than the pointer to None? Is it possible that a micro benchmark would create that illusion while the opposite is true in more realistic environments?
Or is it that eq actually must involve indirections that instanceOf do not?
BR
John
Hmm, I'm curious. Why would instanceOf be faster than eq?
In the end I would expect both to end up being no more than a pointer comparison. If not I'd say there's a bug somewhere.
I get the following, almost identical bytecode with not an instanceof anywhere to be seen. I have no idea what is going on there. Possibly the compiler emits this code as an optimization.
--
No, I changed NoneValue to private object NoneValue following the suggestion from Sébastien. So there would be a type to check against.
Hi all,
I think it is clear that if you want to replace scala.Option with a value class, you have to support Some(null). There is just no way around it even if it might be ugly.
On Wednesday, April 24, 2013 3:18:49 PM UTC-7, rklaehn wrote:Hi all,
I think it is clear that if you want to replace scala.Option with a value class, you have to support Some(null). There is just no way around it even if it might be ugly.
My thoughts are to have a class that is Option-like that does not have Some(null), since that is the heart of the issue for use as a tool for dealing with null with full type safety. Support of Some(null) means that Option is not null-safe. I simply want a type that encodes whether the value can be null or not, and forces me to handle it appropriately.
As Paul has said a few times, the root of the problem is that Option conflates two things: Is there a result? Is the value null? These are not the same thing.
I'll call the thing that only answers the question "Is the value null?" and therefore does not support Some(null) "Opt" to be like a prior similar proposal.
The only constructor in this hypothetical Opt[A] is Opt(val: A). If null is passed in it becomes a None, otherwise a Some. Pattern matching with Some either would not be supported, or fail if null was passed in. I'm thinking that Some could be hidden from the user entirely, but have not worked through it. Opt(null) evaluates to None. Opt(None) also evaluates to None (since it is null in disguise). Opt(Opt(x)) also must equal Opt(x) for it to work as a value class and faithfully be merely "is it null?". This causes some functor/monad issues.