-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to put a contracts on a hashmap's keys and values #42
Comments
Don't have syntax for this right now. Closest I think is something like this: class HashMap
# non-implementation of a HashMap :)
get: (key) -> @[key]
put: (key, val) -> @[key] = val
HashMapC = (keyContract, valContract) ->
?{
get: ((keyContract) -> valContract)
put: (keyContract, valContract) -> Any
}
StrHashMap = HashMapC Str, Str
f :: (StrHashMap) -> Any
f = (map) ->
map.get 4
m = new HashMap
m.put 4, "the number four"
f m # contract violation... Does this match with what you've been trying? We definitely need some nicer syntax for stuff like this. I'm leaning towards the parameterisation approach ( |
I think the problem I was just going to open up a ticket for is somewhat related. I am extending backbone views and models. The preferred way of doing this is View = Backbone.View.extend
method: (args) -> So all the methods are defined in a JSON style. It isn't obvious to me how to add a contract. I can define the method externally, but that leads to some duplication. Perhaps I should add it on after extending? |
I think you should be able to do something like: View :: {
method: (C) -> C
}
View = Backbone.View.extend
method: (args) -> ... |
yeah, I know I should be able to do that but then my type definition would be far away from the function location. Backbone views can easily take up 100 lines of code. |
Ah, I see. Yeah, there's probably not a great way to do it at the moment. I'd like to add the ability to define inline contracts with object literals. So one could write: obj =
(Str) -> Str
meth1: (x) -> ...
(Num) -> Num
meth2: (x) -> ...
Num
x: 42 This is related to adding support for classes since the grammar here is pretty similar. Hopefully I'll have some time soon to crank this out :) But this still wouldn't entirely solve your backbone example. If we added inline contracts or tried this today: spec :: { method : (C) -> C }
spec = { method: (args) -> ... }
View = Backbone.View.extend spec so the object we pass Not sure the best way to address this. |
method :: (C) -> C
method = (args) -> ...
# create a new copy and manually add methods
View = Backbone.View.extend {}
mkView = () ->
v = new View
v.method = method
# call to the outside function
View = Backbone.View.extend
method : (args...) -> method(args...) These seem like the only approach where I get a contract next to my function and place the contract on the correct object. |
This seems to work well. A small amount of manageable boilerplate. I need the context, though, so it looks like: View = Backbone.View.extend
method : (args...) -> method(this, args...) I could apply the context (and maybe I will in the future), but I actually kind of like passing it around. |
The style I am using now is addContractMethods = (singleton) ->
singleton.method :: (Str) -> Any
singleton.method = (str) ->
mkClass contractMethods |
How to put a contracts on a hashmap's keys and values, for example custom key contract and object values.
Right now it's achivable via a custom function contracts which:
Is there/will be a syntax for this? How about contract parameters? Maybe
Or
The text was updated successfully, but these errors were encountered: