simplification of go variable intialization syntax [duplicate] - arrays

This question already has answers here:
golang shortcut syntax for slice literal of pointers to structs
(1 answer)
GoLang Empty Struct Construction
(1 answer)
How is it that I can initialize a slice of pointers like this?
(1 answer)
Go: Confusion about initialization of struct array [duplicate]
(1 answer)
Closed 7 days ago.
In the code below:
var v1 []string = []string{"1"}
var v2 map[string][]string = map[string][]string{"a": []string{"1"}}
The second one can be simplified as:
var v2 map[string][]string = map[string][]string{"a": {"1"}}
while the first can not be written as:
var v1 []string = {"1"}
I don't know in which version is this simplified syntax added in Go, and where it can be used, why the []string one cannot be simplified?

https://go.dev/ref/spec#Composite_literals says:
Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T.
Related: Create array of array literal in Golang

Related

How to create an Array or Dictionary whose values can only be String, Int and Boolean? [duplicate]

This question already has answers here:
Generic dictionary value type in Swift
(1 answer)
Swift Dictionary With Expansive Type Constraints
(1 answer)
Is it possible to specify the object type allowed in a Dictionary?
(2 answers)
Closed 5 years ago.
I have a requirement where I need to create an array whose values can only be either String, Int or boolean.
Swift compiler should complain if I tried to add Double or any other value type.
protocol Elem {}
extension Int: Elem {}
extension String: Elem {}
extension Bool: Elem {}
let arr = [Elem]()
You can do it by declaring a dummy protocol
protocol SpecialType {}
and let conform the requested types to that protocol
extension String : SpecialType{}
extension Int : SpecialType{}
extension Bool : SpecialType{}
Now the compiler complains if you try to add a Double
let specialDict : [String:SpecialType] = ["1" : "Hello", "2": true, "3": 2.0]
// value of type 'Double' does not conform to expected dictionary value type 'SpecialType'

Why cant swif3 initialize Array of Nested class the usual way? [duplicate]

This question already has answers here:
Why can't I instantiate an empty array of a nested class?
(2 answers)
Why can't I use the short Array constructor syntax when creating an Array of a nested Struct? [duplicate]
(1 answer)
Closed 5 years ago.
Let me share the code.
public class A {
public class B { }
}
Now when i want to make a array of type B. I would natuarally use this syntax.
let intarr = [Int]()
let c = [A.B]() //cannot call value of non-function type A.B.Type
However, swift wont allow me to use array the above way. But, the following 2 ways doesnot error.
let c1 = Array<A.B>()
let c2: [A.B] = []
The question now is: why is the first expression let c = [A.B]() not valid?

Which protocol I should conform to add one generic value to another in Swift? [duplicate]

This question already has answers here:
What protocol should be adopted by a Type for a generic function to take any number type as an argument in Swift?
(2 answers)
Closed 6 years ago.
I've created a struct called 'Code', which contains a string and generic property. So, now I'm trying to create a method, which is going to work with the array of Code elements. But the problem is that I'm not aware about the protocol that T-elements should conform to perform + operator.
struct Code<Element>{
let probability: Element
let code: String
}
func createTree<T>(_ array: [Code<T>]){
for i in 0..<array.count-1{
let sum = array[i].probability + array[i+1].probability
}
}
Thanks for your help!
Arithmetic is the protocol you are after. See https://developer.apple.com/reference/swift/arithmetic

Assigning optional to nil in array [duplicate]

This question already has answers here:
Can't assign an string to an array element inside my Swift function
(2 answers)
Closed 7 years ago.
I am trying to create a method that takes an array of CGPoint optionals, and assigns each of them to nil. I am getting an error "Cannot assign a value of type nil to a value of type CGPoint?". I am confused because I thought that optionals could either be nil or some type of specified object. Thanks for any help (:
func resetArray(arr: [CGPoint?])
{
for index in 0...arr.count-1
{
arr[index] = nil
}
}
When a parameter is passed to a function, it is immutable, so you are not allowed to change it. For arrays, being value types, that means you cannot use any mutating method or otherwise perform any action that changes the value.
You can make the parameter writeable by prefixing it with the inout modifier:
func resetArray(inout arr: [CGPoint?])
^^^^^
Note that, when invoking the function, the parameter must be passed by prefixing it with the & ampersand character:
var array = [...]
resetArray(&array)
^
You can also make a parameter modifiable, but with the changes visible from within the function body only (which means changes occur on a copy of the original parameter passed to the function) - that can be done by using the var modifier:
func resetArray(var arr: [CGPoint?])
^^^

Is int[] is a object? [duplicate]

This question already has answers here:
Is int[] an object? [duplicate]
(4 answers)
Closed 8 years ago.
Suppose int[] a = new int[]; It will create a new object or not ?
and one more question .
if int[] a = { 1 , 4 , 5 , 6 } Here it will create a new object or not ? if it's creating a >new object then how it will be created
If you are talking about Java then the answer is Yes it will create a new object
The Java Language Specification section 4.3.1 clearly says that:
An object is a class instance or an array.
Also,
In the Java programming language arrays are objects (§4.3.1), are
dynamically created, and may be assigned to variables of type Object
(§4.3.2). All methods of class Object may be invoked on an array.
Arrays are object only. Arrays is the object that holds references of primitive or object
Java Arrays are Object(s) with syntatical sugar.
int [] a = new int[2];
a[0] = 1;
a[1] = 2;
System.out.println(Arrays.toString(a));
You can also declare the array
int [] a = {1,2};
Regardless of how you declare the array, it is an Object. Is has a length field, additionally you can cast it to an Object,
int[] a = { 1, 2 };
Object o = a;
System.out.println(o.getClass().getName());
It does output the unfortunately named,
[I
You can use the Array utility from the reflect package,
System.out.println(Array.get(a, 0));
From the Array Javadoc,
The Array class provides static methods to dynamically create and access Java arrays.

Resources