Comments (1)
That's a good question.
In this example, this is the expected behavior at the moment.
Since the Hardcore api is used, Nfun is obliged to fully determine the types of input variables by the time of launch.
In the first example, a = x[0] + x[1]
- x
will be defined as real[]
In the second example, a = func(x[0]) + func(x[1])
- x
will be defined as int[]
Explanation
Since all we know is that the type of an array element can be added - that is, it is some kind of arithmetic type (int32
uint32
int64
uint64
real
). Since nothing else is known, Nfun uses the most common type - real
.
Btw - if we created a custom function in the script f(x) = x[0]+x[1]
, then such a function would be generalized. But the body of the script must be solved without any generics
In the second case, we see that the type of the array element is used only as int32
How to fix
if we are sure that x is always int[], then we can tell nfun about it via the apriori api:
var runtime = Funny.Hardcore
.WithApriori<int[]>("x")
.Build("y = x");
If we do not know this before compiling the script (only the user can set this), then this can be specified in the script in one of the ways:
either a:int = x[0] + x[1]
either x:int[]; a = x[0] + x[1]
Or initialize the variable value with the correct type
runtime["x"].Value = new double[] { 1, 1 };
But in difficult cases, this may require additional verification:
if(runtime["x"].Type.Equals( FunnyType.ArrayOf(FunnyType.Int32))
// do assignment here
Why doesn't Nfun do the conversion itself?
You can make your example work, but the question arises - does the user understand exactly what he is doing? After all, if int[]
is installed in the script where real[]
is expected, then it turns out that we will simply hide the problem - after all, the user gets an unexpected type and sooner or later, on a more complex example, it can all break in the most unexpected way. I'm not sure yet that we need to change the current behavior. Maybe improve the error message. But this is a debatable point (I'm waiting for additional arguments)
from nfun.
Related Issues (20)
- Consider to rollback project to .netstandard 2.0 HOT 3
- Update nuget package to latest master version. HOT 1
- how can use in .netframework 4.5 HOT 1
- Allow to use '=' instead of '== ' in single line anonymous expression
- Improve Readme.md
- Support Multi-expression runtime node
- Allow to use ',' instead of ';'
- Allow multiple inputs declaration
- Failed to compare text samples
- ip address template
- Allow to serialize .net objects to nfun script
- NFun allows to access non existing fields
- Custom types cannot be used as output types in Hardcore mode HOT 1
- Primitive type common ancestor in If expression
- Exception: (Expected: UInt16, but was: UInt16) HOT 3
- "--" is not allowed HOT 4
- How to use Dictionary<string,object> or List or Array as variables input? HOT 1
- How to cast(or convert) to some type ? HOT 1
- How to compare System.Version? HOT 1
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 nfun.