I am trying to create a Bit type - I'm not interested in efficiency - I'm playing with a proof of bitstrings with XOR as an Abelian Group.
Why are \ttone and \ttzero invalid characters? I'm getting them from https://docs.julialang.org/en/v1/manual/unicode-input/
fails:
julia> @enum 𝔹 𝟶 𝟷
ERROR: syntax: invalid character "𝟶" near column 9
Stacktrace:
[1] top-level scope
@ none:1
julia> typeof(𝟷)
ERROR: syntax: invalid character "𝟷" near column 8
Stacktrace:
[1] top-level scope
@ none:1
works:
julia> @enum 𝔹 α β
I can't find any contradiction in Julia - Allowed Variable Names
\ttzero = U+1D7F6
seems like a parsing error
number-like chars are usually not allowed to start an identifier
so a𝟶
parses fine
right, \ttone
is not a letter
julia> '𝟷'
'𝟷': Unicode U+1D7F7 (category Nd: Number, decimal digit)
maybe a string macro for creating your "bitstrings" is better suited?
@Sebastian Pfitzner That looks like what the parser was complaining about, but the documentation says:
Allowed Variable Names
Variable names must begin with a letter (A-Z or a-z), underscore, or a subset of Unicode code points greater than 00A0
And then (IMHO) it mumbles a bit...
It does say "Subsequent characters may also include ! and digits (0-9 and other characters in categories Nd/No)", which kinda implies that those are not allowed at the start
My lack of unicode experience is showing... Unicode Categories are now decrypted for me.
List of Unicode Characters of Category “Decimal Number”
Sort of happy with this:
> @enum 𝔹 zero one
> ⊻(a::𝔹, b::𝔹) = a == b ? zero : one
⊻ (generic function with 1 method)
> @show (zero ⊻ one, one ⊻ one)
(zero ⊻ one, one ⊻ one) = (one, zero)
you're kind of punning the functions zero
and one
though
julia is very unicode aware
@Sukera
you're kind of punning the functions
zero
andone
though
I'm not sure what you mean by functions. I thought I was defining a data-type with two elements zero
and one
, which I am giving behavior by defining the function ⊻
.
Cheers...
that will locally shadow the one
/zero
functions that Base exports
which isn't necessarily bad though
If do:
julia> typeof(zero)
Enum 𝔹:
zero = 0
one = 1
The enumeration, to my mind - coincidentally, assigns them integers corresponding to their 'emotional' value.
This works as well:
> @enum 𝔹 zero=3 one=7
> ⊻(a::𝔹, b::𝔹) = a == b ? zero : one
> @show (zero ⊻ one, one ⊻ one)
(zero ⊻ one, one ⊻ one) = (one, zero)
I could have given them boolean values - which I might like to do?
oh boy...
Not sure what to say other than to re-iterate what Sebastian said. one
and zero
are functions exported by Base
julia> zero(Float64)
0.0
julia> one(Complex{Rational{Int}})
1//1 + 0//1*im
When you do
@enum 𝔹 zero one
you're locally changing the meaning of those identifiers to be what your Enum
defines
Which is fine
And you can still access Base.one
and Base.zero
by qualifying the names
julia> @enum 𝔹 zero one
julia> zero(Float64)
ERROR: MethodError: objects of type 𝔹 are not callable
Stacktrace:
[1] top-level scope
@ REPL[5]:1
julia> Base.zero(Float64)
0.0
I made a joke - which I couldn't see :-))
Is there a way of creating a type in Julia, which has a set of elements, without using @enum?
Though now all you kind people have shown me what I was really doing, I think for the purposes of this exercise, I have something that works.
You could use an inner constructor to reject invalid data
julia> struct 𝔹²
x::Int
y::Int
𝔹²(x,y) = if (x,y) ∈ Iterators.product((0,1), (0,1))
new(x,y)
else
throw("Invalid booleans $((x,y))")
end
end
julia> 𝔹²(1, 0)
𝔹²(1, 0)
julia> 𝔹²(0, 0)
𝔹²(0, 0)
julia> 𝔹²(0, 2)
ERROR: "Invalid booleans (0, 2)"
Stacktrace:
[1] 𝔹²(x::Int64, y::Int64)
@ Main ./REPL[8]:7
[2] top-level scope
@ REPL[11]:1
I found this: Style recommendation for enum as type, which has discussion on the difference between enumerated types, and types that can be dispatched apon.
Alexander has marked this topic as unresolved.
Alexander has marked this topic as resolved.
from an out-of-the-box REPL:
julia> zero(true)
false
julia> zero(false)
false
julia> one(false)
true
julia> one(true)
true
Learning by Breaking
Sebastian Pfitzner said:
It does say "Subsequent characters may also include ! and digits (0-9 and other characters in categories Nd/No)", which kinda implies that those are not allowed at the start
"Subsequen t characters may also include ! and digits (0-9 and other characters in categories Nd/No)", implies that characters other than a-z, _, A-Z,!, 0-9, "subset of Unicode code points greater than 00A0" and so on, are not allowed either at the beginning or as subsequent characters, in my opinion.
In any case, the definition seems to me not "clean" as it speaks as allowed characters of UN SUBSET unspecified of Unicode code points greater than 00A0
It is not clear whether all code points greater than 00A0 or only some (which?) of them are intended.
yes, Bool
is a Integer
in julia
which IMO is much more sensible than the reverse where a lot of languages treat integers as booleans
yes, but only if you error when trying to index with a boolean
else writing x > 4
instead of x >> 4
in an indexing expression by accident is horrible to debug
Last updated: Nov 06 2024 at 04:40 UTC