Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly bracket constructors?

Tags:

crystal-lang

While reading through the Crystal docs, I came across this line:

 deq = Deque{2, 3}

So I think this calls the Deque.new(array : Array(T)) constructor. However, I did not find any documentation about this syntax whatsoever. (EDIT: The documentation can be found here)

To test this way of calling constructors, I wrote the following test

class Foo(T)
  def initialize(@bar : Array(T)); end

  def to_s(io : IO)
    io << "Foo: "
    io << @bar
  end
end

puts Foo{1} # Line 10

But, compiling it prints this error:

Error in line 10: wrong number of arguments for 'Foo(Int32).new' (given 0, expected 1)
Overloads are:
 - Foo(T).new(bar : Array(T))

Which I really don't understand at all. Foo(Int32){1} Raises the same error.

Question is, what is this Klass{1, 2, 3} syntax? And how do you use it?

like image 569
Jens Nedregård Avatar asked Mar 14 '18 12:03

Jens Nedregård


People also ask

What is the purpose of {} squiggly braces in Java?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What are curly braces in C++?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true.

What do curly brackets mean in pseudocode?

Curly braces separate a block of code. They are used with many different things including classes, functions, loops, and conditionals. Square brackets are used to index (access) elements in arrays and also Strings. Specifically lost[i] will evaluate to the ith item in the array named lost.

Where do I put my curly brackets in C?

Place each opening curly bracket on the same line as the preceding control flow statement, and place each closing curly bracket on a separate line. The only possible exception to this rule is with else and else if (...) statements.


1 Answers

They are documented here: https://crystal-lang.org/docs/syntax_and_semantics/literals/array.html


Array-like Type Literal

Crystal supports an additional literal for arrays and array-like types. It consists of the name of the type followed by a list of elements enclosed in curly braces ({}) and individual elements separated by a comma (,).

Array{1, 2, 3}

This literal can be used with any type as long as it has an argless constructor and responds to <<.

IO::Memory{1, 2, 3}
Set{1, 2, 3}

For a non-generic type like IO::Memory, this is equivalent to:

array_like = IO::Memory.new
array_like << 1
array_like << 2
array_like << 3

For a generic type like Set, the generic type T is inferred from the types of the elements in the same way as with the array literal. The above is equivalent to:

array_like = Set(typeof(1, 2, 3)).new
array_like << 1
array_like << 2
array_like << 3

The type arguments can be explicitly specified as part of the type name:

Set(Number) {1, 2, 3}
like image 155
Stephie Avatar answered Sep 22 '22 03:09

Stephie