Is MyObject.defaultInstance different from MyObject()?

25 views
Skip to first unread message

Dmytro Starosud

unread,
Oct 17, 2019, 10:40:56 AM10/17/19
to ScalaPB
Unfortunately didn't find this in docs, thus have several related questions.

1. Should one prefer using `MyObject.defaultInstance` over `MyObject()`?

2. Is it possible that for some configuration `MyObject.defaultInstance == MyObject()` will return `false`?

3. Is it possible that for some configuration `MyObject.defaultInstance` will be different from `MyObject()` in any sense (except reference equality off course)?

Nadav Samet

unread,
Oct 17, 2019, 11:12:26 AM10/17/19
to Dmytro Starosud, ScalaPB
Hi Dmytro!

1. MyObject.defaultInstance is a lazy val, so it always gives the same object. MyObject() creates a new instance, and will not compile when you have required files since values for them are required. defaultInstance populates those values with defaults. In normal code, prefer MyObject().

2. It is possible in configurations that would involve some sort of customization. For example, if you change the collection types to Arrays, where different empty instances are not equal. Or when you change a field to custom type that implements `equals` in a way that would be false for different references. I am pretty sure `MyObject.defaultInstance == MyObject()` always holds true if your proto doesn't involve any customizations.

3. Answered in (2).
 
Hope this helps,
Nadav




--
You received this message because you are subscribed to the Google Groups "ScalaPB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalapb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalapb/cffb9f9e-43ee-44ca-89d2-a95c69d0c6c2%40googlegroups.com.


--
-Nadav

Dmytro Starosud

unread,
Oct 17, 2019, 11:28:35 AM10/17/19
to Nadav Samet, ScalaPB
Thanks Nadav for such a quick response!

Could you also please expand a bit possible kinds of customizations. Basically I am wondering in two things.

1. Is it possible that for some setting ScalaPB will generate something like following:

case class MyObject(value: Option[Int] = None) extends GeneratedMessage
object MyObject {
  def defaultInstance: MyObject = MyObject(value = Some(5))
}

In other words is it possible that implementation of `defaultInstance` will be something different than just calling apply with default arguments (or with no arguments at all)?

2. You mentioned messages with required arguments. So for such messages defaultInstance will still be generated, but it will use null, false, 0 for required fields. Right?


чт, 17 жовт. 2019 о 18:12 Nadav Samet <thes...@gmail.com> пише:

Nadav Samet

unread,
Oct 17, 2019, 11:40:31 AM10/17/19
to Dmytro Starosud, ScalaPB
1. A code like the one you provided isn't generated by ScalaPB. There should be nothing surprising between what you get in a constructor and defaultInstance. 

2. Yes, except that `null`s are never used in ScalaPB. You'd see Nones for options, 0 for numeric values, empty strings and so on. If a field is a required message type, it will use the defaultInstance of that message type.
--
-Nadav

Dmytro Starosud

unread,
Oct 18, 2019, 4:27:00 AM10/18/19
to Nadav Samet, ScalaPB
I think it's clear now.

Thank you a lot Nadav
Dmytro



чт, 17 жовт. 2019 о 18:40 Nadav Samet <thes...@gmail.com> пише:
Reply all
Reply to author
Forward
0 new messages