Why does the last line here trigger "Cannot query ...
# community-support
s
Why does the last line here trigger "Cannot query the value of this property because it has no value available"? What am I doing wrong to store a custom property via the object factory?
Copy code
@JvmInline
value class ProjectVersion(val version: String)

objects.property<ProjectVersion>().set(ProjectVersion("oink"))
println(objects.property<ProjectVersion>().get().version)
v
You create a property instance, let's call it
foo
, and set its value to
oink
. Then you create a new property instance, let's call it
bar
, and query its value. Why do you expect
bar
to have the value you have set on
foo
?
s
Ah, crap, I see.
👌 1
v
Speaking in primitives, you do
Copy code
val foo = "bar"
val bar = null
assert bar == foo
🙂
s
But how to I read an unnamed property then via the object factory?
Can I only do that for named properties?
v
What do you mean by "unnamed property"? Actually my primitive example was bad, better is
Copy code
"bar"
assert null == "bar"
😄
objects.property<ProjectVersion>()
gives you a property instance. You then set a value. And then you discard it. You probably meant
Copy code
val foo = objects.property<ProjectVersion>().set(ProjectVersion("oink"))
println(foo.get().version)
(Besides that you of course should never call
get()
at configuration time, because that introduces the same bad race-conditions
afterEvaluate
gives you.
s
Assume that I cannot have
val foo
in our example. Is there no way to gets it's value from
objects
after its creation?
v
objects
is just a factory that creates a new property instance for you
It does not persist the created instances in any way
I'm guessing we are talking about an XY-problem
s
Oh, I thought it's more like a registry of properties...
👎 1
v
What do you actually try to achieve?
s
I'm looking for a way to inject a custom type into the constructor of a custom task. It has to be the constructor I'm injecting to, not a property of the task.
v
So? Just give it as argument to the
register
call
🙂
s
Now we're back to square one: I want to use replace instead of register 😉 I know, you suggested to remove and then register, but I'd really like to avoid that.
v
So you tried to not inject the value to the constructor, but to retrieve it within the constructor body or what?
Well, if you are at bad practices already, set an extra property on the project and get it from there. Or create a build service to which you give the value and get it from the build service.
s
Yeah, I was considering to use a build service. But that's really becoming too complex for something that should be very simple to achieve 😞
v
Well, replacing a task that is already registered is faaar from being simple and shouldn't be done :-)
c
what are the tasks in question and how do they differ in behavior?
s
If it shouldn't be done, why is there a public API for it? But don't get me started on that... IMO Gradle is recently doing a very bad job at making clear what APIs (or better: practices) to use, and what not. All convenient API seems to be discouraged, and all recommended API lacks functionality. With Gradle becoming more and more declarative also, I'm seriously considering to just to back to Maven 😅
The fact that something like https://docs.gradle.org/current/userguide/best_practices.html is even needed says it all. Ideally, the API is designed in a way that you cannot use it in a non-best-practices way...
v
Probably backwards compatibility. Regarding practices, they just launched a yet-to-grow best-practices chapter in the docs
c
I generally find when asking question here it's best to start at as a high a level as possible since you may have walked down a bad path to get to your current problem.
👍 1
v
Well, the problem is backwards compatibility. You don't want to constantly break every build and plugin on every major version just to evolve the API
Maven does not have that so much as a problem because it practically is stale while Gradle is constantly improving 🙂
s
Well, I've been attending the talk about Maven 4 at JavaLand, and was quite pleased with the evolution, I have to say.
But I'm getting too much off-topic.