Giter Club home page Giter Club logo

rackettypeprovider's Introduction

RacketTypeProvider

F#-style Type Providers in the Racket language

What is a Type Provider?

A Type Provider is a construct to aid programmers that are working with large and/or changing datasets, especially those with no explicit type/shape definition such as an XML DTD.

Type Providers integrate with IDEs such as Visual Studio or Dr. Racket to give autocompletion hints when a developers is trying to access part of this data. Additionally, the functions to get, create, or mutate pieces of this data are checked at compile-time for validity. Finally, the compile-time bindings to functions need to be available at runtime, even if the data used at runtime has different contents than the data used while editing.

In short the features of Type Providers are:

  • Compile-time checking of data manipulation.
  • IDE Autocompletion of names of data elements/fields.
  • The ability to get the shape of data while editing code, and automatically adjust to the similarly-shaped contents of a different data source.

Setup

The default Racket package management should be used to install the info.rkt file.

After that, due to lame limitations that I totally should fix, you will need to save any XML files you wish to use to your /Racket folder OR use an absolute path name when specifying the edit-time path within the macro call.

How does it work?

This is a tricky question to answer. An exact answer can be gleaned from the publicly available source code, but the following may help with basic understanding. As with much of this documentation, the example Type Provider used is the XML Type Provider.

The user supplies an edit-time data source to a macro. This is used to infer the type of the data by parsing the XML structure, gathering the names of elements, the names of their fields, and building structs out of them. This edit-time information fuels the autocomplete, and supplies the expansion of the macro as well.

The edit-time data is thus used by the macro in a normal macro-expansion sense, but is also read by the IDE which performs some of the same work as the macro, but does it without actually expanding any macros. The IDE can now tell the user what names of XML data (and their fields) are available with the prefix the user started writing.

The macro, once expanded, will make available a compile-time table of structs behind the scenes. This helps Dr. Racket's background compilation recognize the XML data as structs as sure as if you manually defined each of them yourself. An additional contribution made at compile-time is the generation of a populate-at-runtime function.

A user might not want the program to operate on the same data that they used for edit-time. Perhaps the data that would be useful to work with is updated every 15 minutes on some remote server. In this case, the user can supply the path to a potentially different set of data for use by the program at run-time. So long as the shape does not vary too severely from data supplied at edit-time, the Type Provider will be able to accommodate this run-time data without any need to revise the program every 15 minutes.

Wait, but standard Racket is not typed!

Racket may seem like a strange choice to implement a Type Provider for. In fact, in this language the term is a bit of a misnomer. While F# is able to give you compile-time checking of types because it is an inherently typed language,

On the other hand, Racket can only give you the shape of the data. Knowing the shape of your data means knowing which names are accessible from within which object. Coupled with autocompletion, this is still useful. (One pending feature of this project is to give hints to the probable types of fields based on the edit-time data.)

Use

An example on how to use the Type Provider is available in the xml-type-provider-use.rkt file. This will still require you to copy the .txt file it references to /Racket or modify it to use an absolute path.

Contribution

Please feel free to contribute to this project via forking this repo as with most Open Source Software. I will setup better guidelines as this project continues to develop.

rackettypeprovider's People

Contributors

blackvegetable avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

symbol-research

rackettypeprovider's Issues

Name Mangling should be handled by Macros

I have been told that macro hygiene can be used to handle the problem where elements and attributes can have the same name. If so, this could be used to remove the ugly $ prefix for attributes and $$ prefix for the content attribute. I'm unsure how much work is involved here. If someone knows more about this topic, please chime in.

Type Provider GUI window shouldn't require a mouse click

Currently, after the Type Provider hotkey is pressed (F8), the GUI pops up a window which then requires a mouse click to select which autocompletion option to use. If it is deselected, F8 or spacebar seems to work to get the selections to appear again but this should be available without a mouse click at all.

Interface for generic Type Provider use should be provided

It would be nice to refactor the existing code base into XML specific Type Provider code and code that applies to all Type Providers. This would allow others to reuse a pattern to construct their own Type Providers to handle any data they'd like. This will likely be a lot of work, but should be very valuable to the language as a whole.

Downloading from external source should be supported

Rather than relying on a filepath for a local resource, both the run-time and the edit-time paths should be allowed to be remove paths. Due to other issues which address improving the edit-time setup, this issue could be satisfied by only changing the run-time functionality. Specifically, it would be simple to add another populate-at-runtime style function named populate-at-runtime-remote that expected a remote path to a server and file and performed the download before doing exactly what populate-at-runtime does.

Types of Fields should be inferred

When the Type Provider produces the structs for each element, information about the contents of the edit-time data's field's types could be saved in some fashion. Later, when the auto-complete displays attribute names, it could also display the types that the edit-time file found associated with those fields.

This could be a little tricky if values of different types are found for the same attribute names within different elements of the same name. (This is not a contract violation.) This should be resolved by displaying up to two* types found for that field, or "many-types" for more.

Example:

name : string
salaried : boolean
hired : string or boolean
extra_info : many-types
content : element

Data files should not have to be found in the /Racket folder

Currently, when XML data files are to be used by the TypeProvider, tool.rtk expects them to be in the /Racket folder. This is because the runtime relative path for the GUI is the /Racket folder. (Alternatively, an absolute path could be used to specify the exact location of the file.)

An ideal solution would somehow let tool.rkt determine the runtime path of the file currently being modified and use that for resolving the relative path of the data file.

#lang declaration should indicate Type Provider use

Ideally, the Type Provider tool should not be looking for the exact macro call and filepath to perform its work. The Type Provider's use should follow from a #lang declaration. Anyone with some pointers on how to accomplish this, please chime in.

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.