What do these square brackets do in the constructor?
Minesweeper([List<String> _input]){
//...
}
I've checked the Dart's official documentation in the sections "classes" and "lists" but neither seem to have a reference to such a syntax. I guess it's a "direct initializer"(?) so the _input field is filled without writing it explicitly in the constructor?
[ ] denotes positional optional parameters
{ } denotes named optional parameters
See - What is the difference between named and positional parameters in Dart?
Related
Curious about the syntax used in this example (https://learn.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-command-line) within the file Library.js
My question, is the getJson function returning multiple values without a tuple?
Any link to F# documentation that explains this syntax would be nice. thanks.
open System.Text.Json
let getJson value =
let json = JsonSerializer.Serialize(value)
value, json
My question, is the getJson function returning multiple values without a tuple?
Yes to the first part, no to the second. The comma on the last line makes these two values a tuple.
You may think from online examples that a tuple is like (1, 2), but it’s just as fine to remove the parentheses if the expression is only on one line. In this case, value, json is the tuple.
Parentheses are used to disambiguate the order of evaluation. For instance, 1, “two”, “three” is a three-tuple of an int and two strings, but 1, (“two”, “three”) is a two-tuple of an int and the 2nd type being another two-tuple of two strings.
The Microsoft Learning link appears to always use parentheses in the examples. This post goes a little further, and has a bit more to say on tuple deconstruction as well: https://fsharpforfunandprofit.com/posts/tuples/.
Here’s more on parentheses (thanks Brent!): if it has a comma, it’s a tuple.
Through an online Dart course, I've found some values bracketed with "less than" and "greater than" marks such as "List< E >".
e.g.
List<int> fixedLengthList = new List(5);
I couldn't find a direct answer online, probably because that question was too basic. Could someone explain what those marks exactly indicate? Or any links if possible.
This are generic type parameters. It allows specializations of classes.
List is a list that can contain any value (if no type parameter is passed dynamic is used by default).
List<int> is a list that only allows integer values andnull`.
You can add such Type parameters to your custom classes as well.
Usually single upper-case letters are used for type parameter names like T, U, K but they can be other names like TKey ...
class MyClass<T> {
T value;
MyClass(this.value);
}
main() {
var mcInt = MyClass<int>(5);
var mcString = MyClass<String>('foo');
var mcStringError = MyClass<String>(5); // causes error because `5` is an invalid value when `T` is `String`
}
See also https://www.dartlang.org/guides/language/language-tour#generics
for example If you intend for a list to contain only strings, you can declare it as List<String> (read that as “list of string”)
I can't figure out what's the difference between Curly brace and Square bracket in Groovy/Grails
Example :
[bookInstanceList:Book.list()]
and :
{
subject blank: false
content blank: false, maxSize: 2000
}
can any one help me please?
Thank you
Groovy List and Map :
First one is Map.
[] (Square bracket) in groovy is used for making list or map.
Example of List:
[] - An empty list
[1,2,3,4] – A list of integer values
[‘Angular’, ‘Groovy’, ‘Java’] – A list of Strings
[1, 2, [3, 4], 5] – A nested list
Example of Map:
[ : ] – An Empty map.
[key: "value"] - Map with key and values
Groovy Closure :
The second one is groovy DSL. We can use multiple strategies to create DSL but in Grails domain constraint blocks used groovy closures for this. You can find more details about DSL here and closures here.
As pointed out in the comments the first is a Map and the second is a closure.
They aren't the same or similar in any way. You seem to be confused because you assume the closure is some type of name value pair. Which, in this case, it would appear to be because of the constraints DSL.
To further understand how this closure is processed you would need to dig deeper into the constraints DSL and see how it uses such things as missing methods and missing properties. It's not a simple subject to explain briefly.
I am trying to created a vector of an enumerated type in Ada, but the compiler seems to expect an equality function overload. How do I telll the compiler to just use the default equal function. Here's what I have:
package HoursWorkedVector is new Ada.Containers.Vectors(Natural,DAY_OF_WEEK);
--where Day of week is defined as an enumeration
When I try to compile, I get the message:
no visible subprogram matches the specification for "="
Do I need to create a comparison function to have a vector of an enumerated type? Thanks in advance.
The definition of Ada.Containers.Vectors starts like this:
generic
type Index_Type is range <>;
type Element_Type is private;
with function "=" (Left, Right : Element_Type)
return Boolean is <>;
package Ada.Containers.Vectors is
The meaning of <> in a generic formal function is defined by RM 12.6(10):
If a generic unit has a subprogram_default specified by a box, and the
corresponding actual parameter is omitted, then it is equivalent to an
explicit actual parameter that is a usage name identical to the
defining name of the formal.
So if, as you said in the comments, DAY_OF_WEEK is defined in another package, your instantiation is equivalent to
package HoursWorkedVector is new Ada.Containers.Vectors(Natural, Other_Package.DAY_OF_WEEK, "=");
which doesn't work because the "=" that compares DAY_OF_WEEK values is not visible.
You can include Other_Package."=" in the instantiation, as suggested in a comment. There are at least three ways to make "=" visible, so that your original instantiation would work:
use Other_Package; This will make "=" directly visible, but it will also make everything else defined in that package directly visible. This may not be what you want.
use type Other_Package.DAY_OF_WEEK; This makes all the operators of DAY_OF_WEEK directly visible, including "<", "<=", etc., as well as all the enumeration literals, and any other primitive subprograms of DAY_OF_WEEK that you may have declared in Other_Package. This is probably the favorite solution, unless for some reason it would be a problem to make the enumeration literals visible.
Use a renaming declaration to redefine "=":
function "=" (Left, Right : DAY_OF_WEEK) return Boolean
renames Other_Package."=";
This makes "=" directly visible.
The compiler automatically selects the predefined equality operator:
with
Ada.Containers.Vectors;
package Solution is
type Day_Of_Week is (Work_Day, Holiday);
package Hours_Worked_Vector is
new Ada.Containers.Vectors (Index_Type => Natural,
Element_Type => Day_Of_Week);
end Solution;
Given the following Swift code:
[1,2,3].map{1}
I am getting a Could not find member map. I realize this is somewhat contrived, but I would have thought it should still work? Has anybody run across a reason why this fails?
If you use shorthand argument names in the closure, then you can omit the parameter list (see page 261):
Swift automatically provides shorthand argument names to inline
closures, which can be used to refer to the values of the closure’s
arguments by the names $0, $1, $2, and so on.
If you use these shorthand argument names within your closure
expression, you can omit the closure’s argument list from its
definition, and the number and type of the shorthand argument names
will be inferred from the expected function type.