What is the Julia equivalent of the None value in Python? (As shown here in built-in constants.)
The Julia equivalent of None is the constant nothing: a value that is returned by expressions and functions which don't have anything interesting to return. In both languages, this value is not printed at an interactive prompt when an expression evaluates to it, but is otherwise just a normal value. There's nothing magical about it other than the printing behavior and the fact that people agree by convention that it is the value one returns when there is nothing interesting to return. The type of nothing is called Nothing (and has Cvoid as an alias for use with C functions).
Julia's type system can also express the concept that an expression cannot produce any value – e.g. if it throws an error or is part of a basic block that cannot execute (dead code). The type of an expression that can never produce a value is the empty union type, Union{}: a union of zero types, of which no values are instances. This is distinct from the type of nothing – since nothing is a normal (but uninteresting) value, so it cannot be an instance of Union{}.
See Also:
https://docs.julialang.org/en/v1/manual/faq/#faq-nothing-1
Related
Let's say I'm invoking something like this
Enum.count(list)
And list is not defined in 'upper scope'. In most of languages you'll probably get something like Variable list is undefined, but in Elixir (it comes from Erlang, so I hope it's same behaviour) you'll be getting undefined function list/0 (there is no such import).
What's the difference in Elixir from other (let's say imperative) programming languages in sense of distinction between variable and function?
Also I've noticed you can make a function in module, and if it takes zero arguments, you can call it without parentheses, I was wondering what's special about that. (was answered below by #sabiwara)
Elixir used to consider parentheses optional for all function calls, including 0-arity functions like Kernel.node/0:
iex> node
:nonode#nohost
This behavior has since been deprecated and will emit a compile time warning:
warning: variable "node" does not exist and is being expanded to "node()", please use parentheses to remove the ambiguity or change the variable name
Parentheses for non-qualified calls are optional, except for zero-arity calls, which would then be ambiguous with variables.
But since it would be a breaking change to just change this behavior, it still works and gets interpreted, in your case, as list().
This might change in Elixir 2.0. A discussion on this topic here.
What programming language has a value that is expressible but not denotable. Also what would this imply?
I don't really understand the difference. At the moment I think it means a functional language because then you can't give variables values only point to them?
Is this completely wrong?
According to these lecture notes by David Schmidt:
Expressible values are values that can be produced by expressions in code, like strings, numbers, lambdas/anonymous functions (in languages that support them), etc.
Denotable values are values that can be named (bound to an identifier) and referred to later, like values of variables or named functions.
For example, a language can have syntax for declaring named functions, but no expression syntax for anonymous functions. So (if I understand that correctly) in this language, functions would be denotable but not expressible.
The only example I could find of values that are expressible but not denotable are error values (in some theoretical languages p.11), which can be produced by an expression (like 1/0) but cannot be bound to an identifier (saved in a variable).
(This assumes that the assignment statement propagates the error instead of simply storing the error value in the variable.)
Anonymous types are also somewhat similar. For example in C#, you can define an anonymous object, which has an anonymous type that cannot be bound to an identifier (is not denotable):
// anonymous objects can only be saved into a variable by using type inference
var obj = new { Name = "Farah", Kind = "Human" };
Assume I want to store I vector together with its norm. I expected the corresponding type definition to be straightforward:
immutable VectorWithNorm1{Vec <: AbstractVector}
vec::Vec
norm::eltype(Vec)
end
However, this doesn't work as intended:
julia> fieldtype(VectorWithNorm1{Vector{Float64}},:norm)
Any
It seems I have to do
immutable VectorWithNorm2{Vec <: AbstractVector, Eltype}
vec::Vec
norm::Eltype
end
and rely on the user to not abuse the Eltype parameter. Is this correct?
PS: This is just a made-up example to illustrate the problem. It is not the actual problem I'm facing.
Any calculations on a type parameter currently do not work
(although I did discuss the issue with Jeff Bezanson at JuliaCon, and he seemed amenable to fixing it).
The problem currently is that the expression for the type of norm gets evaluated when the parameterized type is defined, and gets called with a TypeVar, but it is not yet bound to a value, which is what you really need it to be called with, at the time that that parameter is actually bound to create a concrete type.
I've run into this a lot, where I want to do some calculation on the number of bits of a floating point type, i.e. to calculate and use the number of UInts needed to store a fp value of a particular precision, and use an NTuple{N,UInt} to hold the mantissa.
This is similar to my previous question, but a bit more complicated.
Before I was defining a type with an associated integer as a parameter, Intp{p}. Now I would like to define a type using a vector as a parameter.
The following is the closest I can write to what I want:
type Extp{g::Vector{T}}
c::Vector{T}
end
In other words, Extp should be defined with respect to a Vector, g, and I want the contents, c, to be another Vector, whose entries should be the of the same type as the entries of g.
Well, this does not work.
Problem 1: I don't think I can use :: in the type parameter.
Problem 2: I could work around that by making the types of g and c arbitary and just making sure the types in the vectors match up in the constructor. But, even if I completely take everything out and use
type Extp{g}
c
end
it still doesn't seem to like this. When I try to use it the way I want to,
julia> Extp{[1,1,1]}([0,0,1])
ERROR: type: apply_type: in Extp, expected Type{T<:Top}, got Array{Int64,1}
So, does Julia just not like particular Vectors being associated with types? Does what I'm trying to do only work with integers, like in my Intp question?
EDIT: In the documentation I see that type parameters "can be any type at all (or an integer, actually, although here it’s clearly used as a type)." Does that mean that what I'm asking is impossible, and that that only types and integers work for Type parameters? If so, why? (what makes integers special over other types in Julia in this way?)
In Julia 0.4, you can use any "bitstype" as a parameter of a type. However, a vector is not a bitstype, so this is not going to work. The closest analog is to use a tuple: for example, (3.2, 1.5) is a perfectly valid type parameter.
In a sense vectors (or any mutable object) are antithetical to types, which cannot change at runtime.
Here is the relevant quote:
Both abstract and concrete types can be parameterized by other types
and by certain other values (currently integers, symbols, bools, and
tuples thereof).
So, your EDIT is correct. Widening this has come up on the Julia issues page (e.g., #5102 and #6081 were two related issues I found with some discussion), so this may change in the future - I'm guessing not in v0.4 though. It'd have to be an immutable type really to make any sense, so not Vector. I'm not sure I understand your application, but would a Tuple work?
In functional programming languages such as Scala and Haskell, there is an Either type that is often used to represent a successful result or a failure/error object. An either is said to be 'left' when it contains a failure and to be 'right' when it contains a successful result.
Is there a reason why "unsuccessful" is "left" and "successful" is "right"? Why are these directions used in the first place?
From Haskell's Data. Either documentation:
The Left constructor is used to hold an error value and the Right constructor is used to hold a correct value (mnemonic: "right" also means "correct").
This is just a guess, but in Haskell Either is monadic on its second type, which is the type associated with the Right constructor. Generic monadic code will therefore change only the right type, so the left type which remains constant is used to hold the error.
So the main reason would be that swapping the type arguments to Either so that Either a b is Left b or Right a is annoying to read for no real benefit.