jsigbiz / spec

JavaScript signature notation
131 stars 6 forks source link

Function-as-object notation #8

Closed junosuarez closed 9 years ago

junosuarez commented 11 years ago

Given this:

var now = function () { return Date.now() }
now.asDate: function () {  return new Date }

I propose the following to describe now:

now: {
  () => Number,
  asDate: () => Date
}

An unnamed function signature at the top of the object type signature would be the callable function. Other, named properties would be properties on the function object.

Precedent: based loosely on the ES6 proposal for extended object literals.

Raynos commented 11 years ago

I ran into a similar problem. Except I have a type definition for functions which makes this harder

/*  type Callback<T> := (Error, T) => void 
    type Continuable<T> := (Callback<T>) => void

    mongo := (uri: String, options: Object?) => db: {
        (Callback<Db>) => void,
        collection: (name: String) => coll: {
            Continuable<Collection>,
            findOne: (selector: Object, options: Object?) => Continuable<Value>,
            ...
        },
        close: Continuable<void>
    }
*/

For the resulting value from the mongo function (whose name is db) I have defined that it is a function that takes a callback and returns void.

I also defined it has two methods.

For the resulting value from collection function (whose name is coll) I've just said that the value is a continuable and it has at least one method.

I'm not sure which syntax I prefer.

Raynos commented 9 years ago

I've been using

now : {
  asDate: () => Date
} & () => Number

This says now is a member of both { asData: Function } and Function.

If this is good enough for your use case please close this issue :)

junosuarez commented 9 years ago

Existing combinators > special-cased syntax. :+1:

junosuarez commented 9 years ago

This is even more reason to clarify #14 because I just found myself wanting to write

now : () => Number
    & {
      asDate : () => Date
    }
Raynos commented 9 years ago

@jden ;) I know. I wrote it the other way around for a reason :D