Giter VIP home page Giter VIP logo

autotypeparameters.jl's Introduction

Build Status Coverage Status

AutoTypeParameters AutoTypeParameters AutoTypeParameters

AutoTypeParameters

A Julia library to reversibly encode "any" value so that it can be used as a type parameter.

Do I Need This?

You need this if you have an error like:

ERROR: TypeError: apply_type: in Val, expected Type{T}, got XXX

when you try to create a "complicated" dependent type.

That error is (partly) explained here - it occurs because the kinds of things that can be types in Julia is limited. Partly for sensible reasons (you don't want a mutable type), but also because of arbitrary implementation details.

What Does It Do?

This package has two functions - freeze() and thaw() - that translate arbitrary values back and forth into a form that is accepted by Julia.

How Does It Work?

You can choose between two encondings.

Using showall()

By default, freeze() takes the output from showall() and converts it into a Symbol, while thaw() uses eval() to convert it back into a "real" value:

julia> using AutoTypeParameters

julia> freeze("a string")
symbol("ATP \"a string\"")

julia> thaw(eval, symbol("ATP \"a string\""))
"a string"

The advantage of the default approach is that the type parameter is readable. The disadvantage, of course, is that it requires showall() to generate output that eval() can handle.

Using serialize()

Alternatively (eg for values which do not have a useful showall() function), the base64 encoded output from serialize() can be used:

julia> using AutoTypeParameters

julia> freeze("a string"; format=:serialize)
symbol("ATP=JhWGYSBzdHJpbmc=")

julia> thaw(eval, symbol("ATP=JhWGYSBzdHJpbmc="))
"a string"

Warnings

Because this package uses eval() it should not be passed arbitrary values rom an untrusted user.

Example

julia> using AutoTypeParameters

julia> type MyType{N}
           x
       end

julia> MyType{"strings not allowed"}(42)
ERROR: TypeError: apply_type: in MyType, expected Type{T}, got ASCIIString

julia> MyType(N, x) = MyType{freeze(N)}(x)
MyType{N}

julia> MyType("strings not allowed", 42)
MyType{symbol("ATP \"strings not allowed\"")}(42)

julia> extract_type{N}(x::MyType{N}) = thaw(eval, N)
extract_type (generic function with 1 method)

julia> extract_type(MyType("strings not allowed", 42))
"strings not allowed"

autotypeparameters.jl's People

Contributors

andrewcooke avatar tkelman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.