default-value

Accessing a function's default parameter value in Kotlin

ぐ巨炮叔叔 提交于 2019-12-01 21:12:21
问题 Can a function's default parameter value be accessed from a function extension, or anywhere else? fun DieRoll.cheatRoll():Int = roll(min = max -1) fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand() 回答1: No, this is not possible. The default values are not accessible. They are just contained in a bridge-method in the bytecode: fun test(a: Int = 123) { } fun test2() { test() test(100) } Results in the bytecode: public final test(int arg0) { //(I)V <localVar:index=0 , name=this , desc

Variable default value

萝らか妹 提交于 2019-12-01 21:08:00
"It is important to point out that the content field of the template is by default set to null (as Java does with all noninitialized object fields upon creation)." It is from book "JavaSpaces Principles Patterns and Practice" Here is code: public class Message implements Entry { public String content; public Message() { } } I wonder if this is true, because I watched somewhere on internet that this is not true? Yes, this is true, but it may not mean quite what you think it means. All object fields will be initialized to null if no value is specified, but primitive types have other default

Accessing a function's default parameter value in Kotlin

浪尽此生 提交于 2019-12-01 19:25:05
Can a function's default parameter value be accessed from a function extension, or anywhere else? fun DieRoll.cheatRoll():Int = roll(min = max -1) fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand() guenhter No, this is not possible. The default values are not accessible. They are just contained in a bridge-method in the bytecode: fun test(a: Int = 123) { } fun test2() { test() test(100) } Results in the bytecode: public final test(int arg0) { //(I)V <localVar:index=0 , name=this , desc=Lorg/guenhter/springboot/kt/Fun;, sig=null, start=L1, end=L2> <localVar:index=1 , name=a , desc=I,

How to initialize multi-dimensional array with different default value

眉间皱痕 提交于 2019-12-01 16:15:59
I am trying to initialize 2-dimensional array of integer values with -1. When I create a new array, it is automatically filled with 0. I know I can do it with 2 for cycles, but I imagine there should be some way of doing this while the array is being build (so I don't have to go through it two times), so that instead of 0, provided value would be inserted. Is it possible? If not during the initial building of the array, is there some other time or code saving way, or am I stuck with 2 for cycles? With a multidimensional array, the loops are most likely the best approach, unless the array is

Is a property default value of null the same as no default value?

流过昼夜 提交于 2019-12-01 15:39:16
And can I therefore safely refactor all instances of class Blah { // ... private $foo = null; // ... } to class Blah { // ... private $foo; // ... } ? Simple answer, yes. See http://php.net/manual/en/language.types.null.php The special NULL value represents a variable with no value. NULL is the only possible value of type null. You can easily test by performing a var_dump() on the property and you will see both instances it will be NULL class Blah1 { private $foo; function test() { var_dump($this->foo); } } $test1 = new Blah1(); $test1->test(); // Outputs NULL class Blah2 { private $foo = NULL

Setting the initial value of a property when using DataContractSerializer

不问归期 提交于 2019-12-01 15:36:27
If I am serializing and later deserializing a class using DataContractSerializer how can I control the initial values of properties that were not serialized? Consider the Person class below. Its data contract is set to serialize the FirstName and LastName properties but not the IsNew property. I want IsNew to initialize to TRUE whether a new Person is being instantiate as a new instance or being deserialized from a file. This is easy to do through the constructor, but as I understand it DataContractSerializer does not call the constructor as they could require parameters. [DataContract(Name=

Setting the initial value of a property when using DataContractSerializer

假装没事ソ 提交于 2019-12-01 15:18:07
问题 If I am serializing and later deserializing a class using DataContractSerializer how can I control the initial values of properties that were not serialized? Consider the Person class below. Its data contract is set to serialize the FirstName and LastName properties but not the IsNew property. I want IsNew to initialize to TRUE whether a new Person is being instantiate as a new instance or being deserialized from a file. This is easy to do through the constructor, but as I understand it

Difference between ParameterInfo.DefaultValue and ParameterInfo.RawDefaultValue

回眸只為那壹抹淺笑 提交于 2019-12-01 14:30:30
问题 This is a follow-up question of How do I get default values of optional parameters? From documentation, DefaultValue: Gets a value indicating the default value if the parameter has a default value. This property is used only in the execution context. In the reflection-only context, use the RawDefaultValue property instead. The default value is used when an actual value is not specified in the method call. A parameter can have a default value that is null. This is distinct from the case where

How to initialize multi-dimensional array with different default value

 ̄綄美尐妖づ 提交于 2019-12-01 14:26:14
问题 I am trying to initialize 2-dimensional array of integer values with -1. When I create a new array, it is automatically filled with 0. I know I can do it with 2 for cycles, but I imagine there should be some way of doing this while the array is being build (so I don't have to go through it two times), so that instead of 0, provided value would be inserted. Is it possible? If not during the initial building of the array, is there some other time or code saving way, or am I stuck with 2 for

Default value on generic predicate as argument

拥有回忆 提交于 2019-12-01 13:46:15
问题 First time question for me :) I need some way to define a default predicate using a generic on the format Func<T, bool> and then use this as a default argument. Something like this: public bool Broadcast(byte command, MemoryStream data, bool async, Func<T, bool> predicate = (T t) => true) When i do this i get the compile error: Default parameter value for 'predicate' must be a compile-time constant Is there a smooth way of doing this that I am missing or should a make the predicate function