Singleton types can, by definition, not hold any data. They are just empty
objects. They are "singletons" because all objects of such a type are not
only equal (==), but also egal (===), as if they were immutable.

To hold data for a singleton, you'd use a global variable.

-erik

On Wed, Dec 9, 2015 at 6:26 PM, Cristóvão Duarte Sousa <cris...@gmail.com>
wrote:

> Maybe this:
>
> type BadInt end
> const badint = BadInt()
> const _badint_val = 6
>
> Base.(:+)(::BadInt, x) = _badint_val - x
>
> badint + 5   # -5
>
> On Wednesday, December 9, 2015 at 8:51:47 PM UTC, milktrader wrote:
>>
>> Ok, thanks. Can you do the following then?
>>
>> 1. restrict the value of Foo to an Int64?
>> 2. assign the value of 6 to the singleton?
>>
>>
>>
>> On Wednesday, December 9, 2015 at 1:09:08 PM UTC-5, Yichao Yu wrote:
>>>
>>> On Wed, Dec 9, 2015 at 12:50 PM, milktrader <milkt...@gmail.com> wrote:
>>> > Can you provide a Foo example of how this works, with both
>>> construction and
>>> > method definition?
>>>
>>> julia> type Foo
>>>        end
>>>
>>> julia> Base.(:+)(::Foo, x) = x
>>> + (generic function with 175 methods)
>>>
>>> julia> Foo() + 1
>>> 1
>>>
>>> julia> Foo() + "bar"
>>> "bar"
>>>
>>>
>>> >
>>> > On Wednesday, December 9, 2015 at 9:32:36 AM UTC-5, Yichao Yu wrote:
>>> >>
>>> >> On Wed, Dec 9, 2015 at 9:22 AM, milktrader <milkt...@gmail.com>
>>> wrote:
>>> >> > I'd like a somewhat clever example (or boring one for that matter)
>>> that
>>> >> > shows:
>>> >> >
>>> >> > 1. How to create an instance of a singleton type
>>> >>
>>> >> Call the constructor just like any other types.
>>> >>
>>> >> The only special thing about singleton type is that two instance of a
>>> >> mutable singleton type are identical. Other than this, they are
>>> simply
>>> >> types that doesn't have a field.
>>> >>
>>> >> >
>>> >> > 2. How to write methods that use this type in a meaningful way.
>>> >>
>>> >> Just like any other types. As long as you are not comparing them,
>>> they
>>> >> are exactly the same with everything else.
>>> >>
>>> >> >
>>> >> > 3. How it's used in Base code (I seem to recall Void is a singleton
>>> >> > type)
>>> >> >
>>> >> > On Wednesday, December 9, 2015 at 9:02:31 AM UTC-5, tshort wrote:
>>> >> >>
>>> >> >> I'm not sure what you want, either. How about this?
>>> >> >>
>>> >> >> julia> type BadInt{X} end
>>> >> >>
>>> >> >> julia> BadInt{3}()
>>> >> >> BadInt{3}()
>>> >> >>
>>> >> >> julia> f{X}(::Type{BadInt{X}}, y) = X - y
>>> >> >> f (generic function with 1 method)
>>> >> >>
>>> >> >> julia> f(BadInt{10}, 3)
>>> >> >> 7
>>> >> >>
>>> >> >> julia> f{X}(::BadInt{X}, y) = X - y
>>> >> >> f (generic function with 2 methods)
>>> >> >>
>>> >> >> julia> f(BadInt{10}(), 3)
>>> >> >> 7
>>> >> >>
>>> >> >>
>>> >> >> On Wed, Dec 9, 2015 at 8:56 AM, Eric Forgy <eric....@gmail.com>
>>> wrote:
>>> >> >>>
>>> >> >>> Not sure I follow, but does this help?
>>> >> >>>
>>> >> >>> julia> type BadInt
>>> >> >>>        end
>>> >> >>>
>>> >> >>> julia> bi = BadInt()
>>> >> >>> BadInt()
>>> >> >>>
>>> >> >>> julia> typeof(bi)
>>> >> >>> BadInt
>>> >> >>>
>>> >> >>>
>>> >> >>> On Wednesday, December 9, 2015 at 9:46:01 PM UTC+8, milktrader
>>> wrote:
>>> >> >>>>
>>> >> >>>> How do you create an instance of type BadInt then?
>>> >> >>>>
>>> >> >>>> On Wednesday, December 9, 2015 at 7:01:25 AM UTC-5, milktrader
>>> wrote:
>>> >> >>>>>
>>> >> >>>>> Trying to wrap my mind around singleton types to see if they
>>> might
>>> >> >>>>> be
>>> >> >>>>> useful for something I'm working on, but running into some
>>> >> >>>>> confusion. Here
>>> >> >>>>> is an example that I started working with:
>>> >> >>>>>
>>> >> >>>>> julia> type BadInt
>>> >> >>>>>        end
>>> >> >>>>>
>>> >> >>>>> julia> import Base.+
>>> >> >>>>>
>>> >> >>>>> julia> +(x::BadInt, y::Int64) = x - y
>>> >> >>>>> + (generic function with 172 methods)
>>> >> >>>>>
>>> >> >>>>> julia> BadInt() = 2
>>> >> >>>>> BadInt
>>> >> >>>>>
>>> >> >>>>> julia> BadInt + 2
>>> >> >>>>> ERROR: MethodError: `+` has no method matching
>>> +(::Type{BadInt},
>>> >> >>>>> ::Int64)
>>> >> >>>>> Closest candidates are:
>>> >> >>>>>   +(::Any, ::Any, ::Any, ::Any...)
>>> >> >>>>>   +(::Int64, ::Int64)
>>> >> >>>>>   +(::Complex{Bool}, ::Real)
>>> >> >>>>>   ...
>>> >> >>>>>
>>> >> >>>>> As I understand, a singleton type can only take on a single
>>> value.
>>> >> >>>>> What's the utility in supporting this?
>>> >> >>
>>> >> >>
>>> >> >
>>>
>>


-- 
Erik Schnetter <schnet...@gmail.com>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to