Coder Social home page Coder Social logo

Type checking in JS about bosquelanguage HOT 2 CLOSED

microsoft avatar microsoft commented on May 18, 2024
Type checking in JS

from bosquelanguage.

Comments (2)

mrkmarron avatar mrkmarron commented on May 18, 2024

Bosque is statically typed, with records, tuples, objects, union, and (limited) intersection types.

In Bosque the open record type {...} is a record with some number of properties with unknown names. We allow type checks and casts with the syntax:

function foo(arg: {...}): {f:Int, g:Int} | {f:String, g:Bool} | None {
    //check if arg is of type {f:Int, g:Int}
    if(arg->is[{f:Int, g:Int}](arg)) {
        return arg->as[{f:Int, g:Int}](); //cast to type
    }

    return arg->tryAs[{f:String, g:Bool}](); //try cast or return None if it fails
}

Currently we don't have a nice JSON interop story and I would like to do something here. From your comment I assume you are thinking something like:

function foo(data: String): {f: Int, g: Int} {
var check = JSON::is[{f: Int, g: Int}](data); //check if string is JSON format that is convertable to record type
if(check) {
    return JSON::parse[{f: Int, g: Int}](data); //parse the json string as the given record type
}
else {
    return none;
}

from bosquelanguage.

chriskuech avatar chriskuech commented on May 18, 2024

I suppose, though that looks pretty verbose. I'd rather just JSON::parse[{f: Int, g: Int}](data) throw an error/return an optional. If Bosque ever had an interop story with TypeScript or JS modules, the general case of converting an any or unknown type to a stronger type would be much more beneficial--I can't remember the last time I called JSON.parse directly.

from bosquelanguage.

Related Issues (20)

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.