Categories
discuss

Why does short-circuit evaluation work when operator precedence says it shouldn’t?

In JavaScript and Java, the equals operator (== or ===) has a higher precedence than the OR operator (||). Yet both languages (JS, Java) support short-circuiting in if statements:

When we have if(true || anything()), anything() isn’t evaluated.

You can also have the following expression: true || foo == getValue()) – for example in an output statement such as console.log(...);, or in an assignment.

Now, according to operator precedence, short-circuiting shouldn’t happen, as === = == > || in terms of precedence. (In other words, the comparison should happen first, for which getValue() ought to be called, as the equality check has a higher precedence that the OR comparison.) But it does. getValue() isn’t called (as can easily be checked by putting an output statement into its body).

Why (does short circuiting work when the operator precedence says it shouldn’t)?
Or am I confusing matters?

Answer

Or am I confusing matters?

You are. I think it’s much simpler to think about precedence as grouping than ordering. It affects the order of evaluation, but only because it changes the grouping.

I don’t know about Javascript for sure, but in Java operands are always evaluated in left-to-right order. The fact that == has higher precedence than || just means that

true || foo == getValue()

is evaluated as

true || (foo == getValue())

rather than

(true || foo) == getValue()

If you just think about precedence in that way, and then consider that evaluation is always left-to-right (so the left operand of || is always evaluated before the right operand, for example) then everything’s simple – and getValue() is never evaluated due to short-circuiting.

To remove short-circuiting from the equation, consider this example instead:

A + B * C

… where A, B and C could just be variables, or they could be other expressions such as method calls. In Java, this is guaranteed to be evaluated as:

  • Evaluate A (and remember it for later)
  • Evaluate B
  • Evaluate C
  • Multiply the results of evaluating B and C
  • Add the result of evaluating A with the result of the multiplication

Note how even though * has higher precedence than +, A is still evaluated before either B or C. If you want to think of precedence in terms of ordering, note how the multiplication still happens before the addition – but it still fulfills the left-to-right evaluation order too.

Categories
discuss

Failed to decode image. The provided image must be a Bitmap

BitmapDescriptor bmpD = BitmapDescriptorFactory.fromResource(R.raw.podval);

    Log.d("myLog", ":" + bmpD);

    GroundOverlayOptions newarkMap = new GroundOverlayOptions()
            .image(bmpD)
            .position(sydney, 8600f, 6500f);
    Log.d("myLog", ":" + newarkMap);
    GroundOverlay imageOverlay = mMap.addGroundOverlay(newarkMap);

Failed to decode image. The provided image must be a Bitmap. But in log I got :com.google.android.gms.maps.model.BitmapDescriptor@58e0ee6 :com.google.android.gms.maps.model.GroundOverlayOptions@1ea9c27

Also I converted jpg image to bmp, after failing to convert it here

Help please.

Answer

Place your image in the drawable folder, not in the raw folder. Also make sure the resource is a valid image file.

Categories
discuss

Typescript: Object to class

Lets say I have a class C and an Instance of Object O (from JSON).

class C {
  str:string;
  num:number;
}

var o = JSON.parse ("{"num":123, "str":"abc"}");

Is there a way I can assign/initialize an instance of C with o and it checks for undefined values as well as for the type WITHOUT doing it by myself in a copy-constructor/function?

I’d like to get an error or exception if a value is missing/undefined or the type does not match.

Thanks!

Answer

You can use Object.assign:

class C {
    str:string;
    num:number;
}
var o = JSON.parse("{"num":123, "str":"abc"}");
const instance:C = Object.assign(new C(), o);
Categories
discuss

How can I call a method from html attribute in vue js

I have a form and I need to call a method from a placeholder and also from other types of html attribute.

Is there anyways I can call a vue method? Here is what I am trying to do

<input type="text" class="form-control" v-model="user.userName" 
 placeholder=t("un") required> // want to call method t() from the placeholder

It seems this method cannot be called this way. Is there any other ways to achieve this?

And my method is

methods: {
   t(key){
        console.log(key)
        var local='fr';
        return this.trans(key,local);
      }
}

Answer

Use v-bind (https://vuejs.org/v2/api/#v-bind)

<input type="text" class="form-control" v-model="user.userName" 
 v-bind:placeholder="t('un')" required>
Categories
discuss

Failure [INSTALL_CANCELED_BY_USER] on Xiaomi device

I bought a Xiaomi Redmi 4x yesterday and I can’t debug my apps in this device.

I already enabled the Developer Options in the device, switch on to USB Debugging, install the USB Drivers and accepted the RSA debugging fingerprint pop that appears when I plug it on USB port for the first time.

So, I select the “Xiaomi Redmi 4X (Android 6.0 – API 23)” in devices list of Visual Studio, click in Run (or deploy) and see this error:

>Detecting installed packages...
>Removing old runtime: Mono.Android.DebugRuntime...
>Target device is arm64-v8a.
>Installing the Mono shared runtime (debug - 1505313604)...
>10% ... 3520kb of 35035kb copied
>20% ... 7040kb of 35035kb copied
>30% ... 10560kb of 35035kb copied
>40% ... 14016kb of 35035kb copied
>50% ... 17536kb of 35035kb copied
>60% ... 21056kb of 35035kb copied
>70% ... 24576kb of 35035kb copied
>80% ... 28032kb of 35035kb copied
>90% ... 31552kb of 35035kb copied
>100% ... 35035kb of 35035kb copied
> Deployment failed
>Mono.AndroidTools.InstallFailedException: Unexpected install output:   pkg: /data/local/tmp/Mono.Android.DebugRuntime-debug.apk
>Failure [INSTALL_CANCELED_BY_USER]
>
>   at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess(String output, String packageName)
>   at Mono.AndroidTools.AndroidDevice.<>c__DisplayClass94_0.<InstallPackage>b__0(Task`1 t)
>   at System.Threading.Tasks.ContinuationTaskFromResultTask`1.InnerInvoke()
>   at System.Threading.Tasks.Task.Execute()
>Unexpected install output:     pkg: /data/local/tmp/Mono.Android.DebugRuntime-debug.apk
>Failure [INSTALL_CANCELED_BY_USER]
>
>Creating "objDebugupload.flag" because "AlwaysCreate" was specified.
>Creating directory "obj.cache".
>Done building project "MyTestApp.csproj".
>Build succeeded.
>An error occured. See full exception on logs for more details.
>Unexpected install output:     pkg: /data/local/tmp/Mono.Android.DebugRuntime-debug.apk
>Failure [INSTALL_CANCELED_BY_USER]
>
>
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Deploy: 0 succeeded, 1 failed, 0 skipped ==========

This occurs with all my apps and just in this Xiaomi device. In other brand devices and emulators, the debug works normally.

Just to complement, I have this problem http://en.miui.com/thread-544718-1-1.html and even following the instructions, I can’t make the “Install via USB” works.

There is any solution to debug in this device?

Answer

Just to help anyone who have the same problem: it was entirely related to the “Install via USB” option in the Developer mode, that MIUI would not let me activate. After numerous attempts and reboots of the device, it finally activated and the deploy worked at the same time.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..