Coder Social home page Coder Social logo

Comments (7)

erik-kallen avatar erik-kallen commented on August 30, 2024

Yes, the reason is that Script# is wrong:

int? x = 0;
int y = x ?? 1;

should mean y = 0, but the || operator gives y = 1

from saltarellecompiler.

erik-kallen avatar erik-kallen commented on August 30, 2024

Thinking about it, Saltarelle is wrong too. x() ?? y() should not evaulate the second function if x() is non-null. Hmm, I wonder how to solve this.

Probably the best way is to use the || operator if the type is statically known to be of types that don't have falsey values (eg normal objects). For types that do have falsey values (string, number, boolean, others?), something else has to be done. Probably something like var y = coalesce(x(), function() { return y(); }, or even an if statement.

from saltarellecompiler.

rekna1 avatar rekna1 commented on August 30, 2024

I don't think scriptsharp is wrong, if you write the same code in javascript (with x as a number), it behaves the same way...

ok, it depends whether you want the behavior to be the closed to c# of to javascript, that's another discussion...

from saltarellecompiler.

erik-kallen avatar erik-kallen commented on August 30, 2024

Yes, but Saltarelle is designed to follow the C# language specification, not Javascript. This means that whichever code is generated must result in (int?)0 ?? 1 having the value 0. Otherwise I have to write a new specification for a language that is quite similar to C#, but has some semantics taken from Javascript, which is something I do not want to do. See, for example, how I have ensured that variable capture semantics are as they are in C# on the launguage features page: http://www.saltarelle-compiler.com/documentation/supported-c-language-features

from saltarellecompiler.

rekna1 avatar rekna1 commented on August 30, 2024

ok, if you put it that way, I agree... so I continue to use coalesce? will it be ok for my case ?

from saltarellecompiler.

erik-kallen avatar erik-kallen commented on August 30, 2024

As long as you don't do x() ?? y(), it will be OK. In the next version it will generate an if statement for this.

from saltarellecompiler.

erik-kallen avatar erik-kallen commented on August 30, 2024

From 1.3.0 and on the translation will use || if possible (types without falsy values), othwerwise coalesce when the RHS is trivial, or an if statement otherwise.

from saltarellecompiler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.