Set default value for DateTime in optional parameter [duplicate]

There is a workaround for this, taking advantage of nullable types and the fact that null is a compile-time constant. (It’s a bit of a hack though, and I’d suggest avoiding it unless you really can’t.) public void SomeClassInit(Guid docId, DateTime? addedOn = null) { if (!addedOn.HasValue) addedOn = DateTime.Now; //Init codes here } In … Read more

Android Preferences: How to load the default values when the user hasn’t used the preferences-screen?

this question is similar to mine: initialize-preferences-from-xml-in-main-activity Just use this code in onCreate method: PreferenceManager.setDefaultValues(this, R.xml.preference, false); It will load your preferences from XML, and last parameter (readAgain) will guarantee that user preferences won’t be overwritten. That means setting the readAgain argument to false means this will only set the default values if this method … Read more

How to set a Django model field’s default value to a function call / callable (e.g., a date relative to the time of model object creation)

The question is misguided. When creating a model field in Django, you are not defining a function, so function default values are irrelevant: from datetime import datetime, timedelta class MyModel(models.Model): # default to 1 day from now my_date = models.DateTimeField(default=datetime.now() + timedelta(days=1)) This last line is not defining a function; it is invoking a function … Read more

What is the purpose of the default keyword in Java?

It’s a new feature in Java 8 which allows an interface to provide an implementation. Described in Java 8 JLS-13.5.6. Interface Method Declarations which reads (in part) Adding a default method, or changing a method from abstract to default, does not break compatibility with pre-existing binaries, but may cause an IncompatibleClassChangeError if a pre-existing binary … Read more

Is a `=default` move constructor equivalent to a member-wise move constructor?

Yes both are the same. But struct Example { string a, b; Example(Example&& mE) = default; Example& operator=(Example&& mE) = default; } This version will permits you to skip the body definition. However, you have to follow some rules when you declare explicitly-defaulted-functions : 8.4.2 Explicitly-defaulted functions [dcl.fct.def.default] A function definition of the form: attribute-specifier-seqopt … Read more

Why does the Scala compiler disallow overloaded methods with default arguments?

I’d like to cite Lukas Rytz (from here): The reason is that we wanted a deterministic naming-scheme for the generated methods which return default arguments. If you write def f(a: Int = 1) the compiler generates def f$default$1 = 1 If you have two overloads with defaults on the same parameter position, we would need … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)