List in Dart programming - dart-null-safety

List animals = new List();
Keeps giving me an error: The default 'List' constructor isn't available when null safety is enabled.
It has suggestions but I don't understand. Any help would be appreciated

Related

Identifier expected when adding to an array

ArrayList<Qubit> cycl = new ArrayList<Qubit>();
Qubit num0 = new Qubit(4,5,6,64);
cycl.add(num0);
has error identifier expected. Can you please help?
A few things to try:
Try specifying the type of object to go into the ArrayList: ArrayList<Qubit> cycl = new ArrayList<Qubit>();
Make sure you are importing java.util.ArrayList
Make sure you are calling the constructor correctly

Empty Array in Swift?

I am trying to create an empty array in Swift that the user adds on to. It is a very small class because I am just starting a new file. I am using this tutorial for my project which is what the code is based off. Here is the code I tried (It didn't work):
var tasks = task[]()
Here is all of my code in case it is needed:
class TaskManager: NSObject {
var tasks = task[]()
func addTask(name: String){
tasks.append(task(name: name))
}
}
There is an error on the var tasks = task[]() line saying: "Array types are now written the brackets around the element type". I am unsure of how to fix the problem.
How can one create an empty array?
Any input or suggestions would be greatly appreciated.
Thanks in advance.
You have to declare the array in this way:
var tasks = [task]()
It changed in swift from the tutorial you are watching.
The syntactic sugar for an array of Type is [Type] in Swift. You can create an empty array of Tasks like this:
class Task{}
var tasks = [Task]()

Trouble with basic arrays

I am fairly new to programming, and I'm trying to do some work with arrays, but I'm getting an error that I don't know how to fix. Any help would be great!
Error: 1084: Syntax error: expecting colon before leftbracket.
Source: hockeyPP({hockeyPlayers[i]});
Error: 1084: Syntax error: expecting identifier before rightbrace.
Source: hockeyPP({hockeyPlayers[i]});
function eliminateAbsentees():void{
for(var i:int=0; i<=hockeyPlayers.length; i++){
if(hockeyPlayers[i].attendance==true){
hockeyPP.push({hockeyPlayers[i]});
}
}
}
remove { and } surrounding hockeyPlayers[i]. Why you want to used it in this way?
function eliminateAbsentees():void{
for(var i:int = 0; i <= hockeyPlayers.length; i++){
if(hockeyPlayers[i].attendance == true){
hockeyPP.push(hockeyPlayers[i]);
}
}
}
As mentioned by Azzy Elvul, your issue was the curly brackets ( "{}" ) around the array item. You'll see curly brackets in a few places:
Function Declarations
Object Declarations
Class Declarations
Loops
Conditionals
I think there is one more, but that is what I came up with off the top of my head. Basically, when you tried to use this line:
hockeyPP.push({hockeyPlayers[i]});
you tried to declare hockeyPlayers[i] as a new Object (the most basic class in ActionScript, and most languages). You can instantiate the Object class by two ways:
var obj:Object = new Object(); and
var obj:Object = {};
You tried to do the second one, the lazy instantiation. So you tried to declare an object with a property of hockeyPlayers[i] without associating a value with it (the basis of all OOP is property:value pairs).
As that first error said, you are missing a colon for that type of instantiation. If you were to try
hockeyPP.push({hockeyPlayers[i] : null}); //null is what an object is when it has no value
you would not have gotten any errors, as that is the correct way to instantiate an Object. For your needs, though, you just want to push an item from one array to another array. So you do
array2.push( array1[ selectedIndex ] );
I would definitely give the LiveDocs some reading. They can seem daunting, but they are incredibly well written and easy to understand once you start going through them.
LiveDocs - Array
LiveDocs - Object

GAE NDB example useage of Future.wait_all()

sorry for my ignorance but my expectation is that this would work:
from google.appengine.ext import ndb
from models import myModels
delete_futures = []
delete_futures.append(ndb.delete_multi_async(myModels.Kind1.query().fetch(999999, keys_only=True)))
delete_futures.append(ndb.delete_multi_async(myModels.Kind2.query().fetch(999999, keys_only=True)))
ndb.Future.wait_all(delete_futures)
but it throws "TypeError: list objects are unhashable".
perhaps use .extend to create a single list rather then a list of lists?
Wait until all Futures in the passed list are done.
Not expecting your passed list of lists maybe.
delete_futures = []
delete_futures.extend(ndb.delete_multi_async(myModels.Kind1.query().fetch(999999, keys_only=True)))
delete_futures.extend(ndb.delete_multi_async(myModels.Kind2.query().fetch(999999, keys_only=True)))
https://developers.google.com/appengine/docs/python/ndb/futureclass#Future_wait_all
each call to delete_multi_async returns a list of futures, so your delete_futures list is a list of lists. Change your appends to extend and it should work

Why can't I create an array of generic type?

This does not work:
def giveArray[T](elem:T):Array[T] = {
new Array[T](1)
}
But this does:
def giveList[T](elem:T):List[T] = {
List.empty[T]
}
I am sure this is a pretty basic thing and I know that Arrays can behave a bit unusual in Scala.
Could someone explain to me how to create such an Array and also why it doesn't work in the first place?
This is due to JVM type erasure. Manifest were introduce to handle this, they cause type information to be attached to the type T. This will compile:
def giveArray[T: Manifest](elem:T):Array[T] = {
new Array[T](1)
}
There are nearly duplicated questions on this. Let me see if I can dig up.
See http://www.scala-lang.org/docu/files/collections-api/collections_38.html for more details. I quote (replace evenElems with elem in your case)
What's required here is that you help the compiler out by providing some runtime hint what the actual type parameter of evenElems is
In particular you can also use ClassManifest.
def giveArray[T: ClassManifest](elem:T):Array[T] = {
new Array[T](1)
}
Similar questions:
cannot find class manifest for element type T
What is a Manifest in Scala and when do you need it?
About Scala generics: cannot find class manifest for element type T

Resources