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?
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 ...
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.
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.)
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:
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:
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:
There's a few things going on here.
f(x) = "blah"
is one-line syntax forfunction f(x)
"blah"
end
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())
.
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"
.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With