Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a string assigned to a struct in this Julia code?

Tags:

julia

I'm looking at the this thread

I'm trying to understand the following line of code:

struct Foo end
(::Type{Float64})(::Foo) = "not a Float64"

It seems like it assigns a string to a variable of type Foo, but I don't see any variable declaration on the left-hand-side.

What is going on?

like image 212
Roymunson Avatar asked Dec 26 '21 18:12

Roymunson


People also ask

What is a struct in Julia?

Structures (previously known in Julia as "Types") are, for the most (see later for the difference), what in other languages are called classes, or "structured data": they define the kind of information that is embedded in the structure, that is a set of fields (aka "properties" in other languages), and then individual ...

What is mutable struct in Julia?

type and immutable are valid up to julia 0.6, mutable struct and struct are the names of the same objects in julia 0.6 and forward. mutable in mutable struct means that the fields can change - which is actually fairly rarely used so being immutable is the default. mutable struct 's are slower than struct s.

What is the string type in Julia?

The built-in concrete type used for strings (and string literals) in Julia is String. This supports the full range of Unicode characters via the UTF-8 encoding. (A transcode function is provided to convert to/from other Unicode encodings.)

Does @Julia support Unicode characters and strings?

Julia fully supports Unicode characters and strings. As discussed above, in character literals, Unicode code points can be represented using Unicode \u and \U escape sequences, as well as all the standard C escape sequences. These can likewise be used to write string literals:

How to do string concatenation in Julia?

It is one of the most useful string operations and Julia offers a very simple way to achieve it. We can place any characters between the strings as well: Using ‘ *’ operator for direct string concatenation:

Is an array of Char s equivalent to a string in Julia?

In Julia, an array of Char s is not equivalent to a String. The syntax Char (80) creates a single character: We can see that an array of characters is not equivalent to a string (note that characters can also be represented using single quotes): Try defining the string fields in your struct using the String type:


Video Answer


1 Answers

There's a few things going on here.

  1. The line is a method, not a variable assignment. f(x) = "blah" is one-line syntax for
function f(x)
  "blah"
end
  1. The (::Foo) part is actually 1 annotated argument for the method. An argument's name can be omitted, you just can't use the argument in the method body because no variable references it. The argument only dispatches the method. For example, f(::Foo) = "blah" would work for f(Foo()) but not f(Bar()).

  2. The (::Type{Float64}) part instead of a method name indicates this is a functor. (f::F)(x) = x*f.suffix means that instances of struct F can be called like a method F(".net")("something"). The name f isn't a method name and will not exist for you to use like one, it's there to reference the instance of F to be used in the method. The name can be omitted if it's not used in the method (::F)(x) = x*".com".

  3. Type{Float64} is a type whose only instance is Float64, so this means that you can only use this method by calling Float64(Foo()). It seems to me that defining a method Base.Float64(::Foo) = "not a Float64" would be equivalent, I don't actually know why they opted for functor syntax.

When you see annotations surrounded by parentheses, the annotation can only belong to whatever is in the parentheses and to the left. f::Foo means f is annotated with Foo, f(::Foo) means an anonymous argument is annotated with Foo.

like image 104
BatWannaBe Avatar answered Oct 24 '22 06:10

BatWannaBe