Stream: helpdesk (published)

Topic: help with Unicode variables defining a Bit type with @enum


view this post on Zulip Peter Goodall (Nov 16 2021 at 04:09):

I can't find any contradiction in Julia - Allowed Variable Names

\ttzero = U+1D7F6

view this post on Zulip Sukera (Nov 16 2021 at 08:08):

seems like a parsing error

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 08:56):

number-like chars are usually not allowed to start an identifier

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 08:56):

so a𝟶 parses fine

view this post on Zulip Sukera (Nov 16 2021 at 08:59):

right, \ttone is not a letter

julia> '𝟷'
'𝟷': Unicode U+1D7F7 (category Nd: Number, decimal digit)

view this post on Zulip Sukera (Nov 16 2021 at 09:00):

maybe a string macro for creating your "bitstrings" is better suited?

view this post on Zulip Peter Goodall (Nov 16 2021 at 18:34):

@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...

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 18:34):

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

view this post on Zulip Peter Goodall (Nov 16 2021 at 18:38):

My lack of unicode experience is showing... Unicode Categories are now decrypted for me.
List of Unicode Characters of Category “Decimal Number”

view this post on Zulip Peter Goodall (Nov 16 2021 at 18:42):

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)

view this post on Zulip Sukera (Nov 16 2021 at 18:54):

you're kind of punning the functions zero and one though

view this post on Zulip Sukera (Nov 16 2021 at 18:55):

julia is very unicode aware

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:42):

@Sukera

you're kind of punning the functions zero and one 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...

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 19:47):

that will locally shadow the one/zero functions that Base exports

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 19:47):

which isn't necessarily bad though

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:48):

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?

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:52):

oh boy...

view this post on Zulip Mason Protter (Nov 16 2021 at 19:53):

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

view this post on Zulip Mason Protter (Nov 16 2021 at 19:53):

Which is fine

view this post on Zulip Mason Protter (Nov 16 2021 at 19:53):

And you can still access Base.one and Base.zero by qualifying the names

view this post on Zulip Mason Protter (Nov 16 2021 at 19:54):

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

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:55):

I made a joke - which I couldn't see :-))

view this post on Zulip Peter Goodall (Nov 16 2021 at 19:56):

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.

view this post on Zulip Mason Protter (Nov 16 2021 at 20:01):

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

Last updated: Oct 02 2023 at 04:34 UTC