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

Expected type during value synthesis #74

Closed
kaleidawave opened this issue Oct 31, 2023 · 1 comment
Closed

Expected type during value synthesis #74

kaleidawave opened this issue Oct 31, 2023 · 1 comment
Assignees
Labels
checking Issues around checking
Milestone

Comments

@kaleidawave
Copy link
Owner

Currently the mapper functions a parameter is synthesised as generic

declare const array1: Array<number>;
array1.map(function mapper(a) { return a + 2 })

then the function is checked based on the restriction.

A better way would be to synthesise the function with the knowledge/expectation that it is expected to be a function as number => U. This means that the new_function can then create parameter restrictions eagerly.

This could be considered as traditional type inference

@kaleidawave kaleidawave added the checking Issues around checking label Oct 31, 2023
@kaleidawave kaleidawave self-assigned this Oct 31, 2023
@kaleidawave kaleidawave added this to the 0.1.0 milestone Nov 1, 2023
@kaleidawave
Copy link
Owner Author

Added the parameter-argument part with

let base_function = get_constraint(ty, &checking_data.types).unwrap_or(ty);
// TODO fails for ors
// TODO not great, gets parameters here, then
let arguments = if let Type::Function(f, _) | Type::FunctionReference(f) =
checking_data.types.get_type_by_id(base_function)
{
// TODO clone here bad
let parameters = checking_data.types.get_function_from_id(*f).parameters.clone();
arguments
.into_iter()
.enumerate()
.map(|(idx, argument)| {
let expected = parameters.parameters.get(idx).map_or(TypeId::ANY_TYPE, |p| {
get_constraint(p.ty, &checking_data.types).unwrap_or(p.ty)
});
SynthesisedArgument {
spread: argument.spread,
position: A::expression_position(argument.expression)
.with_source(environment.get_source()),
value: A::synthesise_expression(
argument.expression,
expected,
environment,
checking_data,
),
}
})
.collect()
} else {
arguments
.into_iter()
.map(|argument| SynthesisedArgument {
spread: argument.spread,
position: A::expression_position(argument.expression)
.with_source(environment.get_source()),
value: A::synthesise_expression(
argument.expression,
TypeId::ANY_TYPE,
environment,
checking_data,
),
})
.collect()
};

Not great position + overhead of clone. But works ATM

These work though

image

from

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

No branches or pull requests

1 participant