Skip to content
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

Static types #228

Merged
merged 36 commits into from
Apr 10, 2018
Merged

Static types #228

merged 36 commits into from
Apr 10, 2018

Conversation

rowanseymour
Copy link
Member

@rowanseymour rowanseymour commented Apr 4, 2018

Addresses: #215, #218 and #221

@coveralls
Copy link

coveralls commented Apr 4, 2018

Coverage Status

Coverage decreased (-1.1%) to 59.754% when pulling 0da4f70 on static_types into e9181da on master.

XValue

ToString() XString
ToBool() XBool
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToString and ToBool are somewhat different to other conversions since every type should be able to render itself as these without blowing up

case XObject:
return x.Reduce().ToString()
}
panic(fmt.Sprintf("can't convert type %v to a string", value))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using panics here because if those goes wrong we've coded something wrong - i.e. its more serious than giving the user back an XError

XTypeArray
XTypeObject
XTypeError
XTypeNil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need XType anymore? This was mostly added for equality and we are really only supporting equality in excellent for bool and numeric types, so that's vastly simplified. Seems we could probably remove these and just use normal type assertions instead.

@rowanseymour rowanseymour changed the title Static types (WIP/preview) Static types Apr 9, 2018
@@ -41,51 +42,51 @@ var XFUNCTIONS = map[string]XFunction{
"legacy_add": LegacyAdd,

"round": Round,
"round_up": RoundUp,
"round_down": RoundDown,
"round_up": OneNumberFunction("round_up", RoundUp),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicpottier here's what functions look like using wrappers - can brainstorm if there's a better way to do this! Does suck having to repeat the function name so that's something to improve.

lookup, err := types.ToString(v.env, v.Visit(ctx.Expression()))
if err != nil {
return err
context := toXValue(v.Visit(ctx.Atom()))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicpottier would be nice to replace these runtime checks with compile time checks

vars["int1"] = 1
vars["string1"] = "string1"
vars["int2"] = 2
vars := types.NewXMap(map[string]types.XValue{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unlike XArray, users can't create these but we use them internally

}

// NewXResolveError creates a new XError when a key can't be resolved on an XResolvable
func NewXResolveError(resolvable XResolvable, key string) XError {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could create several of these factory methods, so that errors of same type are created the same way. Then at some point we could add error codes or the like to make it easier for consumers like the simulator to localize these

arg1Str := fmt.Sprintf("%v", arg1)
arg2Str := fmt.Sprintf("%v", arg2)
// RequireMarshalToXString calls json.Marshal in the given value and panics in the case of an error
func RequireMarshalToXString(x interface{}) XString {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, though I think the golang convention is must instead of require

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can change - copied Require from decimal.RequireFromString

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, well maybe I'm wrong, maybe worth checking what is used more, no biggie either way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right about Must being more standard - and MustMarshalToXString sounds marginally better

Copy link
Collaborator

@nicpottier nicpottier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to pretend I read / understand all that but it looks awesome. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants