Comments (7)
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.
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.
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.
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.
ok, if you put it that way, I agree... so I continue to use coalesce? will it be ok for my case ?
from saltarellecompiler.
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.
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)
- [2.6.2] Canceled tasks should throw TaskCanceledException instead of InvalidOperationException HOT 3
- Status? HOT 1
- [2.6.2] Combining async, finally and IList/IEnumerable results in an incorrect execution workflow HOT 3
- Bridge.NET Acquires Saltarelle HOT 54
- Boolean XORs generate incorrect result HOT 1
- Non-serializable properties for serializable types HOT 1
- int.Parse() gets translated to parseInt(), which isn't precise HOT 4
- Bug in inheritance? HOT 3
- INTERNAL ERROR: ResolveResult ConversionResolveResult HOT 6
- Add support for InternalsVisibleTo attribute HOT 3
- [2.7.0] Asynchronous tasks swallow exceptions
- Minifier incorrectly minifies '+ ++' into '+++'
- When using [NamedValuesAttribute] on an enum, that enum's values are not compiled correctly when used as default parameters HOT 3
- If the data exict HOT 1
- Saltarelle.Runtime HOT 1
- Saltarelle rewrites Mixing expression $.expr[':'] to $["expr[':']"] HOT 1
- Provide toString override for Exception class, otherwise uncaught exceptions will end up logged as [object Object]
- ss_Int32.tryParse returns 0 when value can't be parsed (when input does not match regexp)
- Dictionary issue on Chrome HOT 3
- Saltarelle.Compiler 2.7.0 - INTERNAL ERROR: System.TypeLoadException
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from saltarellecompiler.