Spring Boot Kotlin entity property val vs var [duplicate] - spring-boot

This question already has answers here:
What is the difference between var and val in Kotlin?
(41 answers)
Closed 1 year ago.
I am learning Spring Boot with kotlin and I am so confused with the usage of var or val for entity property. In some tutorial they use val and the other use var. So I don't know which is the right one to declare property for entity. At least enlightenment me about this. Please & Thanks!!

val and var both are used to declare variables but the main difference between them could be defined as;
val: is used when you declare a variable with a value which you don't want to change or update, it is kind of a constant variable which can only be initialized once. And when you try to change its value, it will show an error like Val can not be reassigned. it is known as immutable variable in kotlin.
On the other hand,
var: is used when you intend to declare a general variable whose value could be changed or updated anywhere in the class. It could be initialized or reassigned multiple times and it is known as mutable variable in kotlin.
I hope this will help you. There is a good definition with example.

Related

What does this Spring Component and Kotlin Class consist of? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have this class here:
#Component
class UpdateRule(
private val rulesRepo: UpdateRuleRepo
) : UpdateRuleStoreGateway, UpdateRuleLoadGateway {
override fun store(rule: UpdatRule) {
//some code
}
override fun loadAll(): List<UpdateRule> {
//some code
}
Since I'm new to Spring and Kotlin, I'm not familiar with this syntax. What does the val inside the brackets mean (private val rulesRepo: UpdateRuleRepo) and what do the interfaces after the colon do (: UpdateRuleStoreGateway, UpdateRuleLoadGateway)? Is this Kotlin specific or Spring specific syntax and where can I read more about it?
This is Kotlin syntax, not Spring-specific.
What does the val inside the brackets mean (private val rulesRepo: UpdateRuleRepo)
The part between the parentheses () is the list of arguments for the primary constructor of the class. When a constructor argument is marked val or var, it also declares a property of the same name on the class.
In this case, it means rulesRepo is a constructor argument of the class UpdateRule, but also a property of that class. Since it's a val, it means the property is read-only (it only has a getter).
You can read more about this in the docs about constructors.
what do the interfaces after the colon do (: UpdateRuleStoreGateway, UpdateRuleLoadGateway)?
The types listed after the : in a class declaration are the parent classes (or interfaces) extended (or implemented) by that class. In this case it means UpdateRule implements both interfaces UpdateRuleStoreGateway and UpdateRuleLoadGateway.
This is described in the docs about implementing interfaces.
Val is var but final to put it simply. Final means it cannot be reassigned to another object/primitive data. But do keep it mind that it doesn't mean internal data is immutable. It's not like Python tuple! Individual element/member variables can be modified, but the whole thing cannot be reassigned(think of variable as a pointer, and it's referencing to the same object, although the internal details of the object may change over time)
Kotlin var is different from Java var by the way.
More on that here: https://www.baeldung.com/kotlin/java-kotlin-var-difference
I personally find kotlin var similar to TypeScript let.

How to store private variables in Substrate storage [duplicate]

This question already has an answer here:
Possibility of private variables in substrate storage
(1 answer)
Closed 2 years ago.
I want to store private variables in Substrate storage and I don't want to encode them. For example I have this structure and I want to store private variable in it.
pub struct TestStruct {
// Some public variables
// Could there be a private variable?
test_private: u64,
}
I understand that according to the idea in the blockchain, everything should be public, and that most likely the variable will need to be encrypted. I heard about the 'reveal pattern', does it help in this case?
I think you need to explain what is your usecase more precisely.
What does private variable means, who has access to it ?
As far as I know 'reveal pattern' is when you send the hash of what you will reveal, and then later you can reveal what was the original value, by checking the on-chain hash.

How to reference Enum inside a Java class from Clojure [duplicate]

This question already has an answer here:
clojure access enum defined inside a java class
(1 answer)
Closed 3 years ago.
How do I reference Enum constant which is enclosed inside a Java class from Clojure? I'm trying to use the field P2PKH from org.bitcoinj.script.Script.ScriptType. See API of bitcoinj.
In a Java interop guide they say:
You can refer to those enumerations in Clojure like this:
DaysOfWeek/TUESDAY
But that doesn't work when the Enum is enclosed in a class. What would be the correct syntax?
You need to use syntax for accessing static inner classes:
OuterClass$InnerClass/staticField
so it should be:
org.bitcoinj.script.Script$ScriptType/P2PKH.

How to define #Value as optional

I have the following in a Spring bean:
#Value("${myValue}")
private String value;
The value is correctly injected. However, the variable needs to be optional, it is passed in as a command line parameter (which is then added to the Spring context using a SimpleCommandLinePropertySource), and this argument will not always exist.
I have tried both the following in order to provide a default value:
#Value("${myValue:}")
#Value("${myValue:DEFAULT}")
but in each case, the default argument after the colon is injected even when there is an actual value - this appears override what Spring should inject.
What is the correct way to specify that #Value is not required?
Thanks
What is the correct way to specify that #Value is not required?
Working on the assumption that by 'not required' you mean null then...
You have correctly noted that you can supply a default value to the right of a : character. Your example was #Value("${myValue:DEFAULT}").
You are not limited to plain strings as default values. You can use SPEL expressions, and a simple SPEL expression to return null is:
#Value("${myValue:#{null}}")
If you are using Java 8, you can take advantage of its java.util.Optional class.
You just have to declare the variable following this way:
#Value("${myValue:#{null}}")
private Optional<String> value;
Then, you can check whether the value is defined or not in a nicer way:
if (value.isPresent()) {
// do something cool
}
Hope it helps!
If you want to make the configuration property optional just pass an empty string like this:
#Value("${app.optional.value:}")
I guess you are you using multiple context:property-placeholder/ declarations?
If so, this is a known issue since 2012, but not fixed, apparently due to both lack of interest and no clean way of fixing it. See https://github.com/spring-projects/spring-framework/issues/14623 for discussion and some ways to work around it. It's explained in an understandable way by http://www.michelschudel.nl/wp/2017/01/25/beware-of-multiple-spring-propertyplaceholderconfigurers-and-default-values/

Use Class Variables As Constants In Scala

I'm working to learn Scala--coming from a C++ background. I am trying
to write a small class for a task tracking app I'm hacking together to
help me to learn how to code Scala.
This seems as if it should be simple but for some reason it's eluding me:
package com.catenacci.tts
class Task(val ID:Int, val Description:String) {
val EmptyID = 0
val EmptyDescription = "No Description"
def this() = this(EmptyID,EmptyDescription)
def this(ID:Int)={
this(ID,EmptyDescription)
}
def this(Description:String)={
this(EmptyID,Description)
}
}
I'm trying to provide three constructors: Task(ID, Description),
Task(ID), Task(Description). In the latter 2 cases I want to
initialize the values to constant values if one of the values isn't
provided by the caller. And I want to be able to check this outside
of the class for unit testing purposes. So I figured putting in two
public vals would allow me to check from outside of the class to make
sure that my state is what I expect.
However, for some reason this code will not compile. I get the following error:
error: not found: value EmptyID
and
error: not found: value EmptyDescription
So what am I missing? I'm working through "Programming in Scala" so
if there's a simple answer to this question, please give me page
numbers. I don't mind reading but going by the code on page 60 and
page 62, I can't see why this code is failing.
I'm guessing it has something to do with the fact that these are
constructor methods and that possibly the two vals are not initialized
until the end of the constructors. If that's the case is there some
way to get the effect I'm looking for?
You can define the constants in a companion object:
object Task {
val EmptyID = 0
val EmptyDescription = "No Description"
}
And then reference them as Task.EmptyID and Task.EmptyDescription.
I think Scala 2.8 has support for default values.
See Germán for the answer. This happens because a constructor is technically part of the static scope. In other words, the constructor cannot access any instance members because the instance hasn't been created yet. Any "class members" are actually instance members, which is why the code in the question does not work. Germán's answer fixes this by moving the two relevant values into the companion object, which effectively makes them static members of the Task class (not really, but you can think of it that way).
In "Programming in Scala", see section 6.7 where the chaining of constructor calls is explained. The primary constructor is described as "the single point of entry of a class".

Resources