Setting Default Values (Conditional Assignment)

Conditional assignment of default values in yang

There are no conditional default values in YANG - you need two default statements for two defaults with different values, and a single leaf may only have one default substatement. You can work around this, however. Perhaps by using a presence container instead of your protocol leaf:

module conditional-default {
namespace "http://example.com/conditional-default";
prefix "excd";

grouping common {
leaf port {
type int32;
}
}

container config {

container ssh {
presence "If this container is present, ssh is configured.";
uses common {
refine port {
default 22;
}
}
}
container http {
presence "If this container is present, http is configured.";
uses common {
refine port {
default 80;
}
}
}

}

}

From RFC6020, 7.5.5.:

The "presence" statement assigns a meaning to the presence of a
container in the data tree. It takes as an argument a string that
contains a textual description of what the node's presence means.

Conditional passing of a default argument to a function (using the '?' operator)

When you are calling the function with out specifiying the parameter, then the default is added at the call site, so when you call

CalculateTimeSilence();

Then you are actually calling

CalculateTimeSilence(-1.f);

There is no default or similar mechanism to get the default argument, but you can do that "manually"

const float default_value = -1.f;

CalculateTimeSilence(float SilenceThresholdOverride = default_value);

and then you can call it as desired:

CalculateTimeSilence(bUseOverride ? OverrideValue : default_value);

However, while the conditional operator comes in handy sometimes, often it obfuscates code and makes it difficult to read. In this case I'd perhaps rather write

auto x = bUseOverride ? OverrideValue : default_value;
CalculateTimeSilence(x);

Set a default value conditionally

From the API docs

after_find and after_initialize callback is triggered for each object that is found and instantiated by a finder, with after_initialize being triggered after new objects are instantiated as well.

You could do something along the lines of:

class Room < ApplicationRecord
after_initialize :set_room_defaults

def set_room_defaults
if people_in_room.size == 1
set room_type = "Single"
elsif people_in_room.size == 2
set room_type = "Double"
elsif people_in_room.size == 3
set room_type = "Triple"
end
end
end

Or you could store the mappings in an Array:

class Room < ApplicationRecord
ROOM_TYPES = %w(Single Double Triple).freeze

after_initialize :set_room_defaults

def set_room_defaults
set room_type = ROOM_TYPES[people_in_room.size - 1]
end
end

Assigning default values to shell variables with a single command in bash

Very close to what you posted, actually. You can use something called Bash parameter expansion to accomplish this.

To get the assigned value, or default if it's missing:

FOO="${VARIABLE:-default}"  # If variable not set or null, use default.
# If VARIABLE was unset or null, it still is after this (no assignment done).

Or to assign default to VARIABLE at the same time:

FOO="${VARIABLE:=default}"  # If variable not set or null, set it to default.

Assign a default value when using the NEW Null-Conditional operator shorthand feature in .Net?

You can use the Or operator.
This operator determines whether the variable is valid, and if it's not, assign the or'ed value.

In your case, you could use:

Dim x = customer.Address.Country Or "No Address"

Instead of

Dim x = If(customer.Address is nothing, "No Address", customer.Address?.Country)

Of course, that does mean these variables can have multiple types; you should perform additional checks to ensure the different object types do not break your program.

Another example (DomainId is 1):

Dim num = System.Threading.Thread.GetDomainID() Or 0
Console.WriteLine(CStr(num))
Console.Read()

The console writes out 1, as it's valid

However if we switch it around so 0 Or System.Threading.Thread.GetDomainID() is used, we'll still get 1 as 0 isn't seen as 'valid'.

If both values are valid, then the rightmost variable is used.

How do i set default value with conditional in laravel migrate?

I invite you to create an observer on your Eloquent model.

You can read the doc at: https://laravel.com/docs/9.x/eloquent#observers

Remember to create a PHP enum to check your roles. This will allow you to more easily add roles or do additional checks in your code without having magic values:

<?php
enum Role : string
{
case ADMIN = 'admin';
case CUSTOMER = 'customer';
}

The creating event seems to be the most suitable since it will be observed during insertion :

<?php

namespace App\Observers;

use App\Models\User;

class UserObserver
{
/**
* Handle the User "creating" event.
*
* @param \App\Models\User $user
* @return void
*/
public function creating(User $user)
{
// Your logic here
if(str_ends_with($user->email, '@domain.com')) {
$user->role = Role::ADMIN->value;
} else {
$user->role = Role::CUSTOMER->value;
}
}
}

Standard approach to assign default value in Java

Try this:

if (myVariable == null) {
myVariable = defaultValue;
}

Set a default parameter value for a JavaScript function

From ES6/ES2015, default parameters are in the language specification.

function read_file(file, delete_after = false) {
// Code
}

just works.

Reference: Default Parameters - MDN

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

In ES6, you can simulate default named parameters via destructuring:

// the `= {}` below lets you call the function without any parameters
function myFor({ start = 5, end = 1, step = -1 } = {}) { // (A)
// Use the variables `start`, `end` and `step` here
···
}

// sample call using an object
myFor({ start: 3, end: 0 });

// also OK
myFor();
myFor({});

Pre ES2015,

There are a lot of ways, but this is my preferred method — it lets you pass in anything you want, including false or null. (typeof null == "object")

function foo(a, b) {
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 'default_b';
...
}


Related Topics



Leave a reply



Submit