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

Implement explicit type conversion #104

Open
joshcarp opened this issue May 10, 2024 · 4 comments
Open

Implement explicit type conversion #104

joshcarp opened this issue May 10, 2024 · 4 comments

Comments

@joshcarp
Copy link

I have implemented var.(<type>) conversions on my fork.

// vars
a := 1
b := a.(any)
c := b.(num)
d := a.(string) // fails
a:any
a = 1
a = "b"
print (typeof a) // prints underlying type, not `any`. Consistent with current behaviour

// Maps
{a: true b: false}.({}any)
{a: true b: false}.({}any).({}bool)
{a: true b: false}.({}string) // fails
{a: true b: "false"}.({}bool) // fails

// Arrays
print [1 2 3].([]any).([]num).([]any).([]num)
x:[]any
x = [[1 2 3]]
print x
assertEqual "[]any" (typeof x)
x[0] = [7 8 9]
print "x:" x
x[0] = [7 8 9].([]string)
print "x:" x
assert false
[7 8 9].([]string) // fails
@joshcarp
Copy link
Author

Honestly i think this would be one of the biggest improvements to the language in its current form. Type conversions always trip me up and it isn't getting any more familiar

@juliaogris
Copy link
Member

Yeah - I also noticed some friction in this space playing with your huaman eval examples - could you please post a couple of examples where in specific you are missing this feature, @joshcarp ?

@joshcarp
Copy link
Author

The main one was with asserts. An empty type is always instantiated as an any slice/map so

a := [1 2 3] // returned from whatever function
a = a[0:0] // empty num slice
print (typeof []) (typeof a) // []any []num

So this basically happens every time you need to:

  • Assign an empty list
  • Assign an empty map

@juliaogris
Copy link
Member

@juliaogris juliaogris transferred this issue from evylang/evy May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants