Type error when using List<int> variable as an argument to a List<double> method parameter - dart

I'm learning the dart language, and I encountered this problem with lists.
I created a sum function to calculate the sum of a list, here is the code:
double sum(List<double> elements) {
var el = 0.0;
for (var elem in elements) {
el += elem;
}
return el;
}
And I call it from the main menu in 2 ways:
void main(List<String> args) {
var sm = sum([1, 3, 4, 6]);
print(sm)
}
And it worked fine. But when I try to use a middle variable:
var test = [1, 3, 4, 6];
var sm = sum(test);
print(sm);
I get an error :
Error: The argument type 'List<int>' can't be assigned to the parameter type
'List<double>'.
functions.dart:5
- 'List' is from 'dart:core'.
var sm = sum(test);
^
I know that i have to use List as i'm using list of int but it appears that that function I made could work with both types, double and int, but i can't understand the problem when I use a middle variable?

In your first example the list of int literals is automatically converted to double, which is a new feature of Dart 2.1. (source) - int literals get converted automatically to doubles in a double context:
var sm = sum([1, 3, 4, 6]);
Before that, you would experience a compilation error and would have to explicitly provide a double list as a parameter:
var sm = sum([1.0, 3.0, 4.0, 6.0]);
On your second example however, you are implicitly defining a List<int> variable, which cannot be passed as a List<double> parameter.

Related

Calling C function from Swift shows different results in CLion than in Xcode [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I am trying to call a C function from Swift , but I do not know exactly how to define variables to pass parameters. This is the function declaration:
/* Function Declarations */
extern void compute_feature_set(const double input[11025],
double M_data[], int M_size[2],
double D_data[], int D_size[2],
double DD_data[],
int DD_size[2],
double VT[10], double *mp,
double r_42[42], double *fM);
The data is an array of floats. So I tried ​​:
let s = data.compactMap{ Double($0)}
var mSize = Array<Int32>(repeating:Int32(0.0), count:2)
var dSize = Array<Int32>(repeating:Int32(0.0), count:2)
var dD_Size = Array<Int32>(repeating:Int32(0.0), count:2)
var mData = Array<Double>(repeating:0.0, count:48)
var dData = Array<Double>(repeating:0.0, count:48)
var dD_Data = Array<Double>(repeating:0.0, count:48)
var vt = Array<Double>(repeating:0.0, count:10)
var mp = Double(0.0)
var r = Array<Double>(repeating:0.0, count:42)
var fM = Double(0)
compute_feature_set(s, &cout, &mSize, &vx, &dSize, &dD_Data, &dD_Size, &vcta, &mp, &r, &fM)
When I run the code in Clion with the following function it works fine and the output matches the expected values:
static void main_compute_feature_set(void)
{
static double dv[11025];
double DD_data[48];
double D_data[48];
double M_data[48];
double r_42[42];
double VT[10];
double fM;
double mp;
int DD_size[2];
int D_size[2];
int M_size[2];
/* Initialize function 'compute_feature_set' input arguments. */
/* Initialize function input argument 'input'. */
/* Call the entry-point 'compute_feature_set'. */
argInit_11025x1_real_T(dv);
compute_feature_set(dv, M_data, M_size, D_data, D_size,
DD_data, Dd_size, VT,
&mp, r_42, &fM);
}
However, when I run my implementation in Swift, I get very different results.
You could try passing pointers of the Arrays, rather than the Arrays directly.
Using Imported C Functions in Swift | Apple Developer Documentation
Call Functions with Pointer Parameters
Whenever possible, Swift avoids giving you direct access to pointers. When importing C function parameters, however, Swift maps pointer parameters to standard library pointer types.
The following tables use Type as a placeholder type name to indicate syntax for the mappings.
For return types, variables, and arguments, the following mappings apply:
C Syntax
Swift Syntax
const Type *
UnsafePointer<Type>
Type *
UnsafeMutablePointer<Type>
double[] is pretty much equivalent to double * in this case.
Looks like the problem with your code is passing data to your function. You use compactMap to make an Array of Double and then pass the pointer of this array. But Array and Double are struct in Swift so you pass the pointer of struct with structs instead of array of double values.
To convert your data to array of bytes you should use withUnsafeBytes e.g.:
Swift:
let data = Data([0xaa, 0xbb, 0xcc, 0xdd])
data.withUnsafeBytes {
passData($0)
}
C/ObjC:
void passData(const double input[11025]) {
NSLog(#"%x", input[0]); // Prints: ddccbbaa
}

Why a const list can't be used in a string literal when it can't be modified in any way?

void main() {
const list = [1, 2, 3];
const string = 'This is a $list'; // Error
}
When I can't assign list a new value and modify any of its elements, why can't I then use the list in my string literal?
Dart doesn't have a concept of saying that a method call can be evaluated at compilation time (in contrast to constexpr in C++). Therefore Dart cannot guarantee that calling a method on a const object returns another const object, and that includes the implicit call to .toString() when doing string interpolation.
For example, this is perfectly legal:
import 'dart:math';
final random = Random();
class Foo {
const Foo();
// Returns a string that is clearly not a compile-time constant.
#override
String toString() => random.nextInt(100).toString();
}
void main() {
const foo = Foo();
print('$foo');
const list = [foo, foo, foo];
print('$list');
}
Note that this doesn't apply to .toString() implementations for some built-in types (e.g. null, numeric, string, and boolean types) since they are known to produce constant values and because Dart does not allow creating derived classes from those types, so they cannot be overridden to do shenanigans like the above example.
It's an interesting question, because some const things can be interpolated into const strings. I checked out the Language Tour, and it gives this example, which is very close to your question:
// These work in a const string.
const aConstNum = 0;
const aConstBool = true;
const aConstString = 'a constant string';
// These do NOT work in a const string.
var aNum = 0;
var aBool = true;
var aString = 'a string';
const aConstList = [1, 2, 3];
const validConstString = '$aConstNum $aConstBool $aConstString';
// const invalidConstString = '$aNum $aBool $aString $aConstList';
The explanation given is that
Literal strings are compile-time constants, as long as any
interpolated expression is a compile-time constant that evaluates to
null or a numeric, string, or boolean value.
They don't explain why (what the technical reasons are, or the motivations for making the language work this way), but it's clear that a const list does not evaluate to "null or a numeric, string, or boolean value." So it just doesn't matter that your list is const—the string resulting from interpolation with that will not be a compile-time constant, so you can't use const with that string.

Why can a variable declared using 'var' be null in null-safe dart?

I'm copying this from the docs
// In null-safe Dart, none of these can ever be null.
var i = 42; // Inferred to be an int.
String name = getFileName();
final b = Foo();
But I'm running the code below in a null-safe dartpad, and it compiles.
void main() {
var x = null;
print(x);
}
Is this a documentation error or am I missing something?
Your example are not close to what the documentation are trying to explain. Try this instead:
void main() {
var x = 42;
x = null; // Error: A value of type 'Null' can't be assigned to a variable of type 'int' - line 3
print(x);
}
The reason is that var x = 42 is "Inferred to be an int" and not int?.
In your example, what happens is that var x = null are resolved so x are seen as the type dynamic since Dart have no clue about what type you are trying to use. Since dynamic can have the value null you are good to go.

What is the difference between the "const" and "final" keywords in Dart?

What is the difference between the const and final keywords in Dart?
There is a post on dart's website and it explains it pretty well.
Final:
"final" means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables.
Const:
"const" has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.
Const objects have a couple of interesting properties and restrictions:
They must be created from data that can be calculated at compile time. A const object does not have access to anything you would need to calculate at runtime. 1 + 2 is a valid const expression, but new DateTime.now() is not.
They are deeply, transitively immutable. If you have a final field containing a collection, that collection can still be mutable. If you have a const collection, everything in it must also be const, recursively.
They are canonicalized. This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated.
So, what does this mean?
Const:
If the value you have is computed at runtime (new DateTime.now(), for example), you can not use a const for it. However, if the value is known at compile time (const a = 1;), then you should use const over final. There are 2 other large differences between const and final. Firstly, if you're using const inside a class, you have to declare it as static const rather than just const. Secondly, if you have a const collection, everything inside of that is in const. If you have a final collection, everything inside of that is not final.
Final:
final should be used over const if you don't know the value at compile time, and it will be calculated/grabbed at runtime. If you want an HTTP response that can't be changed, if you want to get something from a database, or if you want to read from a local file, use final. Anything that isn't known at compile time should be final over const.
With all of that being said, both const and final cannot be reassigned, but fields in a final object, as long as they aren't const or final themselves, can be reassigned (unlike const).
Const
Value must be known at compile-time, const birthday = "2008/12/25" Can't be changed after initialized.
Final
Value must be known at run-time, final birthday = getBirthDateFromDB() Can't be changed after initialized.
Consolidated #Meyi and #faisal-naseer answers and Comparing with little programming.
const:
const keyword used to make a variable to store a compile time constant value. Compile time constant value is a value which will be constant while compiling :-)
For example 5 is a compile time constant. While DateTime.now() which is not compile time constant. Because this method will return the time when the line is getting executed at runtime. So we can't assign the DateTime.now() to a const variable.
const a = 5;
// Uncommenting below statement will cause compile time error.
// Because we can't able to assign a runtime value to a const variable
// const b = DateTime.now();
Should be initialized at the same line.
const a = 5;
// Uncommenting below 2 statement will cause compilation error.
// Because const variable must be initialized at the same line.
// const b;
// b = 6;
All statements mentioned below are acceptable.
// Without type or var
const a = 5;
// With a type
const int b = 5;
// With var
const var c = 6;
Class level const variable should be initialized like below.
Class A {
static const a = 5;
}
Instance level const variable is not possible.
Class A {
// Uncommenting below statement will give compilation error.
// Because const is not possible to be used with instance level
// variable.
// const a = 5;
}
The another major use of const is used to make the object immutable. To make a class object immutable we need to use the const keyword with constructor and make all the fields as final like mentioned below.
Class A {
final a, b;
const A(this.a, this.b);
}
void main () {
// There is no way to change a field of object once it's
// initialized.
const immutableObja = const A(5, 6);
// Uncommenting below statement will give compilation error.
// Because you are trying to reinitialize a const variable
// with other value
// immutableObja = const A(7, 9);
// But the below one is not the same. Because we are mentioning objA
// is a variable of a class A. Not const. So we can able to assign
// another object of class A to objA.
A objA = const A(8, 9);
// Below statement is acceptable.
objA = const A(10, 11);
}
we can use const keyword to a list.
const a = const [] - A variable a initialized as const which contains a list of const objects(i.e., The list should contain only compile time constant and immutable objects). So we can't able to assign a with another list.
var a = const [] - A variable a initialized as var which contains a list const objects. So we can able to assign another list to the variable a.
Class A {
final a, b;
const A(this.a, this.b);
}
class B {
B(){ // Doing something }
}
void main() {
const constantListOfInt = const [5, 6, 7,
// Uncommenting below statement give compilation error.
// Because we are trying to add a runtime value
// to a constant list
// DateTime.now().millisecondsSinceEpoch
];
const constantListOfConstantObjA = const [
A(5, 6),
A(55, 88),
A(100, 9),
];
// Uncommenting below 2 statements will give compilation error.
// Because we are trying to reinitialize with a new list.
// constantListOfInt = [8, 9, 10];
// constantListOfConstantObjA = const[A(55, 77)];
// But the following lines are little different. Because we are just
// trying to assign a list of constant values to a variable. Which
// is acceptable
var variableWithConstantList = const [5, 6, 7];
variableWithConstantList = const [10, 11, 15];
var variableOfConstantListOfObjA = const [A(5, 8), A(7, 9), A(10, 4)];
variableWithConstantList = const [A(9, 10)];
}
final:
final keyword also used to make the variable to hold a constant value. Once initialized we can't change the value.
final a = 5;
// Uncommenting below statement will give compilation error.
// Because a is declared as final.
// a = 6;
All statements mentioned below are acceptable.
// Without type or var
final a = 5;
// With a type
final int b = 5;
// Can't use var along with final keyword. Uncommenting below line cause compilation issue.
// final var c = 6;
Able to assign a runtime value.
// DateTime.now() will return the time when the line is getting
// executed. Which is a runtime value.
final a = DateTime.now();
var b = 5;
final c = b;
Class level final variable must be initialized in the same line.
Class A {
static final a = 5;
static final b = DateTime.now();
}
Instance level final variable must be initialized in the same line or in the constructor initialization. The value will be put into memory when the object is created.
Class A {
final a = 5;
}
// Constructor with a parameter.
Class B {
final b;
B(this.b);
}
// Constructor with multiple parameter.
Class C {
final c;
C(this.c, int d) {
// Do something with d
}
}
void main() {
A objA = new A();
B objB = new B(5);
C objC = new C(5, 6);
}
Assigning a list.
final a = [5, 6, 7, 5.6, A()];
// Uncommenting Below statement will give compilation error.
// Because we are trying to reinitialize the object with another list.
// a = [9.9, 10, B()];
Extending the answer by #Meyi
final variable can only be set once and it is initialized when
accessed.(for example from code section below if you use the value of biggestNumberOndice only then the value will be initialized and memory will be assigned).
const is internally final in nature but the main difference is that
its compile time constant which is initialized during compilation
even if you don't use its value it will get initialized and will take
space in memory.
Variable from classes can be final but not constant and if you want a
constant at class level make it static const.
Code:
void main() {
// final demonstration
final biggestNumberOndice = '6';
// biggestNumberOndice = '8'; // Throws an error for reinitialization
// const
const smallestNumberOnDice = 1;
}
class TestClass {
final biggestNumberOndice = '6';
//const smallestNumberOnDice = 1; //Throws an error
//Error . only static fields can be declared as constants.
static const smallestNumberOnDice = 1;
}
const means its initial value is must be fixed, can not be a dynamic value;
final means its initial value is must be fixed but can be a dynamic value, equal to the var with a fixed value.
code demos
const
void main() {
const sum = 1 + 2;
// ✅ const can not change its value
print("sum = ${sum}");
// ⚠️ Const variables must be initialized with a constant value.
const time = new DateTime.now();
// ❌ Error: New expression is not a constant expression.
print("time = ${time}");
}
final
// new DateTime.now();
// dynamic timestamp
void main() {
final sum = 1 + 2;
// ✅ final can not change its value
print("sum = ${sum}");
final time = new DateTime.now();
// ✅ final === var with fixed value
print("time = ${time}");
}
Screenshots
refs
https://dart.dev/guides/language/language-tour#final-and-const
final and const in dart are confusing to the level we think both of them are the same.
Let's see their differences :
P.S. I included image instead of text as I couldn't tabulate the info
in Stackoverflow .md format easily.
Both final and const prevent a variable from being reassigned (similar to how final works in Java or how const works in JavaScript).
The difference has to do with how memory is allocated. Memory is allocated for a final variable at runtime, and for a const variable at compile-time. The final modifier should be the more commonly used, because many program variables won't need any memory since the program logic won't call for them to be initialized. With a const variable you are basically telling the computer, "Hey, I need memory for this variable up front because I know I'm going to need it."
Thinking of them in this way makes it easier to understand differences in their syntactical usage. Mainly that a final variable may be an instance variable, but a const must be a static variable on a class. This is because instance variables are created at runtime, and const variables--by definition--are not. Thus, const variables on a class must be static, which means simply that a single copy of that variable exists on a class, regardless of whether that class is instantiated.
This video breaks it down fairly simply:
https://www.youtube.com/watch?v=9ZZL3iyf4Vk
This article goes into more depth and explains a very important semantic difference between the two, i.e. final modifies variables and const modifies values, which essentially boils down to only being able to initialize const values which are derivable at compile-time.
https://news.dartlang.org/2012/06/const-static-final-oh-my.html
Anything that isn't known at compile time should be final over const.
If you are coming from C++ then const in Dart is constexpr in C++ and final in Dart is const in C++.
The above applies to primitive types only.
However in Dart objects marked final are mutable in terms of it's members.
Simple words:
Const
Value must be known at compile-time, i.e. values coming from internal files.
Sample: API keys, supported languages of your app or any variables in your i.e. helper file, basically anything which you ship with your app.
Final
Value must be known at run-time.
It can be data like above but also i.e. device info which will be checked when the app is starting or data that is loaded from API's or servers when the app starts, but before the app is ready to use i.e. you need to check if the user is logged in or not, your app would load or check a session token from the server.
The answer is in the image itself.
const is a Compile-time constant.
final is a Run-time constant.
All these answers I can describe in a concise way.
const list = [1, 2, 3];
Both variable/identifier & value are const. Like - const list = const [1, 2, 3]
That's why they are not allowed to be re-assigned.
Good fit for global variables.
It is possible to use it as a class variable but have to set static. Like - static const list = [1, 2, 3].
vs:
final list = [1, 2, 3];
Variable/Identifier is const but value is not. Like - const list = [1, 2, 3]
That's why we can perform like - list.add(4)
You can't initialise a const using a final. For example :
final myConst = 1;
const myFinal = 2;
final a = myConst; // possible
final b = myFinal; // possible
const c = myConst; // this is not possible
const d = myFinal; // possible
When to use which keyword?
A simple example for both:
Use final: If you don’t know what it’s value will be at compile-time. For example, when you can need to get data from an API, this happens when running your code.
Use const: If you are sure that a value isn’t going to be changed when running your code. For example, when you declare a sentence that always remains the same.
https://itnext.io/difference-between-const-and-final-in-dart-78c129d0c573
A variable with the final keyword will be initialized at runtime and can only be assigned for a single time.
A variable with the const keyword is initialized at compile-time and is already assigned when at runtime.
Use final: If you don’t know what it’s value will be at compile-time. For example, when you can need to get data from an API, this happens when running your code.
Use const: If you are sure that a value isn’t going to be changed when running your code. For example, when you declare a sentence that always remains the same.

Cannot assign to a parameter

I have declared a function
func someFunction(parameterName: Int) {
parameterName = 2 //Cannot assign to let value parameter Name
var a = parameterName
}
and trying to assign it a value during runtime, but it gives me error
"Cannot assign to let value parameter Name".
Is the parameter name constant by default? Can I change it to a variable?
[In Swift >= 3.0] Function parameters are defined as if by let and thus are constants. You'll need a local variable if you intend to modify the parameter. As such:
func someFunction (parameterName:Int) {
var localParameterName = parameterName
// Now use localParameterName
localParameterName = 2;
var a = localParameterName;
}
[In Swift < 3.0] Declare the argument with var as in:
func someFunction(var parameterName:Int) {
parameterName = 2;
var a = parameterName;
}
use of inout has a different semantics.
[Note that "variable parameters" will disappear in a future Swift version.] Here is the Swift documentation on "variable parameters":
Function parameters are constants by default. Trying to change the
value of a function parameter from within the body of that function
results in a compile-time error. This means that you can’t change the
value of a parameter by mistake.
However, sometimes it is useful for a function to have a variable copy of a parameter’s value to work with. You can avoid defining a
new variable yourself within the function by specifying one or more
parameters as variable parameters instead. Variable parameters are
available as variables rather than as constants, and give a new
modifiable copy of the parameter’s value for your function to work
with.
Define variable parameters by prefixing the parameter name with the keyword var: ..."
Excerpt From: Apple Inc. “The Swift Programming Language.”
If you actually want to change the value stored in a location that is passed into a function, then, as #conner noted, an inout parameter is justified. Here is an example of that [In Swift >= 3.0]:
1> var aValue : Int = 1
aValue: Int = 1
2> func doubleIntoRef (place: inout Int) { place = 2 * place }
3> doubleIntoRef (&aValue)
4> aValue
$R0: Int = 2
5> doubleIntoRef (&aValue)
6> aValue
$R1: Int = 4
In order to modify the argument passed in, you have to designate it as an inout parameter:
func someFunction(inout parameterName:Int)
{
parameterName = 2;
var a = parameterName;
}
Note this will change the variable that was passed in as well. If that isn't what you're looking for, use var as GoZoner suggested.

Resources