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

Add support for generic function declarations #2463

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

turbolent
Copy link
Member

Description

It turns out that adding basic support for generic functions does not take much effort, as we already have support for generic built-in functions.

  • Remove the type parameter lists feature flag in the parser, enabling them constantly
  • Add support function declarations with type parameter lists.

Even with the following limitations, this feature should already make some programs safer and allow developer to express their intent better:

  • For now, type parameters may only occur in the parameter list and the return type.

    We may add support for type parameter occurrences in the function body later, which would require some work in the interpreter.

  • Interface function declarations may not be generic (yet).


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels
    …clarations

@github-actions
Copy link

github-actions bot commented Apr 23, 2023

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit d7f3648
The command for i in {1..N}; do go test ./... -run=XXX -bench=. -benchmem -shuffle=on; done was used.
Bench tests were run a total of 7 times on each branch.

Collapsed results for better readability

old.txtnew.txt
time/opdelta
CheckContractInterfaceFungibleTokenConformance-2115µs ± 0%113µs ± 0%~(p=1.000 n=1+1)
ContractInterfaceFungibleToken-244.6µs ± 0%37.9µs ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsCCF-2146ms ± 0%214ms ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsJSON-2489ms ± 0%750ms ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.FeesDeducted-24.37µs ± 0%3.09µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.TokensWithdrawn-23.71µs ± 0%2.40µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-23.71µs ± 0%3.21µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-23.36µs ± 0%3.37µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.NewWeeklyPayout-22.43µs ± 0%2.45µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowIDTableStaking.RewardsPaid-22.90µs ± 0%2.96µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensDeposited-22.91µs ± 0%2.94µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensDeposited_with_nil_receiver-22.85µs ± 0%2.89µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensMinted-22.40µs ± 0%2.40µs ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowToken.TokensWithdrawn-22.96µs ± 0%2.95µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowFees.FeesDeducted-212.1µs ± 0%12.3µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowFees.TokensWithdrawn-27.00µs ± 0%7.00µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-210.7µs ± 0%10.8µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-215.1µs ± 0%15.2µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.NewWeeklyPayout-27.02µs ± 0%7.09µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowIDTableStaking.RewardsPaid-29.14µs ± 0%9.26µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensDeposited-210.3µs ± 0%9.7µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensDeposited_with_nil_receiver-28.77µs ± 0%8.78µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensMinted-27.08µs ± 0%7.16µs ± 0%~(p=1.000 n=1+1)
DecodeJSON/FlowToken.TokensWithdrawn-29.83µs ± 0%9.72µs ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsCCF-2117ms ± 0%74ms ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsJSON-2129ms ± 0%166ms ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.FeesDeducted-21.52µs ± 0%1.54µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.TokensWithdrawn-21.10µs ± 0%1.11µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-21.56µs ± 0%1.59µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-21.67µs ± 0%1.67µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.NewWeeklyPayout-21.10µs ± 0%1.11µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowIDTableStaking.RewardsPaid-21.42µs ± 0%1.44µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensDeposited-21.51µs ± 0%1.51µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensDeposited_with_nil_receiver-21.48µs ± 0%1.49µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensMinted-21.10µs ± 0%1.10µs ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowToken.TokensWithdrawn-21.50µs ± 0%1.51µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowFees.FeesDeducted-23.05µs ± 0%3.81µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowFees.TokensWithdrawn-21.70µs ± 0%1.68µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-22.75µs ± 0%2.96µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-23.85µs ± 0%3.84µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.NewWeeklyPayout-21.73µs ± 0%1.75µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowIDTableStaking.RewardsPaid-22.29µs ± 0%2.30µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowToken.TokensDeposited-22.67µs ± 0%2.69µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowToken.TokensDeposited_with_nil_receiver-22.08µs ± 0%2.07µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowToken.TokensMinted-21.71µs ± 0%1.71µs ± 0%~(p=1.000 n=1+1)
EncodeJSON/FlowToken.TokensWithdrawn-22.66µs ± 0%2.60µs ± 0%~(p=1.000 n=1+1)
ExportType/composite_type-2350ns ± 0%347ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-253.9ns ± 0%52.0ns ± 0%~(p=1.000 n=1+1)
InterpretRecursionFib-22.49ms ± 0%2.36ms ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_interpreter-21.11µs ± 0%1.17µs ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-2586ns ± 0%591ns ± 0%~(p=1.000 n=1+1)
ParseArray-28.03ms ± 0%8.02ms ± 0%~(p=1.000 n=1+1)
ParseDeploy/byte_array-211.8ms ± 0%12.0ms ± 0%~(p=1.000 n=1+1)
ParseDeploy/decode_hex-21.20ms ± 0%1.20ms ± 0%~(p=1.000 n=1+1)
ParseFungibleToken/With_memory_metering-2181µs ± 0%184µs ± 0%~(p=1.000 n=1+1)
ParseFungibleToken/Without_memory_metering-2144µs ± 0%146µs ± 0%~(p=1.000 n=1+1)
ParseInfix-27.20µs ± 0%7.00µs ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/One_level-22.34ns ± 0%2.34ns ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/Three_levels-2136ns ± 0%140ns ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-25.03ms ± 0%5.01ms ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-23.96µs ± 0%3.89µs ± 0%~(p=1.000 n=1+1)
SuperTypeInference/arrays-2317ns ± 0%313ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/composites-2134ns ± 0%134ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/integers-298.5ns ± 0%97.8ns ± 0%~(p=1.000 n=1+1)
ValueIsSubtypeOfSemaType-291.3ns ± 0%89.5ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-249.2kB ± 0%49.2kB ± 0%~(p=1.000 n=1+1)
ContractInterfaceFungibleToken-223.3kB ± 0%23.3kB ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsCCF-267.3MB ± 0%67.4MB ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsJSON-2246MB ± 0%246MB ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.FeesDeducted-21.41kB ± 0%1.41kB ± 0%~(all equal)
DecodeCCF/FlowFees.TokensWithdrawn-21.22kB ± 0%1.22kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-21.49kB ± 0%1.49kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-21.50kB ± 0%1.50kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.NewWeeklyPayout-21.26kB ± 0%1.26kB ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.RewardsPaid-21.38kB ± 0%1.38kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited-21.34kB ± 0%1.34kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited_with_nil_receiver-21.33kB ± 0%1.33kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensMinted-21.22kB ± 0%1.22kB ± 0%~(all equal)
DecodeCCF/FlowToken.TokensWithdrawn-21.35kB ± 0%1.35kB ± 0%~(all equal)
DecodeJSON/FlowFees.FeesDeducted-26.03kB ± 0%6.03kB ± 0%~(all equal)
DecodeJSON/FlowFees.TokensWithdrawn-23.62kB ± 0%3.62kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-25.46kB ± 0%5.46kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-27.40kB ± 0%7.40kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.NewWeeklyPayout-23.67kB ± 0%3.67kB ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.RewardsPaid-24.57kB ± 0%4.57kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited-24.93kB ± 0%4.93kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited_with_nil_receiver-24.50kB ± 0%4.50kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensMinted-23.63kB ± 0%3.63kB ± 0%~(all equal)
DecodeJSON/FlowToken.TokensWithdrawn-24.91kB ± 0%4.91kB ± 0%~(all equal)
EncodeBatchEventsCCF-240.3MB ± 0%40.3MB ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsJSON-234.0MB ± 0%34.0MB ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.FeesDeducted-2808B ± 0%808B ± 0%~(all equal)
EncodeCCF/FlowFees.TokensWithdrawn-2688B ± 0%688B ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-2872B ± 0%872B ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-2848B ± 0%848B ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.NewWeeklyPayout-2704B ± 0%704B ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.RewardsPaid-2848B ± 0%848B ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited-2816B ± 0%816B ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited_with_nil_receiver-2800B ± 0%800B ± 0%~(all equal)
EncodeCCF/FlowToken.TokensMinted-2688B ± 0%688B ± 0%~(all equal)
EncodeCCF/FlowToken.TokensWithdrawn-2816B ± 0%816B ± 0%~(all equal)
EncodeJSON/FlowFees.FeesDeducted-2768B ± 0%768B ± 0%~(all equal)
EncodeJSON/FlowFees.TokensWithdrawn-2408B ± 0%408B ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-2760B ± 0%760B ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-2952B ± 0%952B ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.NewWeeklyPayout-2424B ± 0%424B ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.RewardsPaid-2624B ± 0%624B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited-2680B ± 0%680B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited_with_nil_receiver-2544B ± 0%544B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensMinted-2416B ± 0%416B ± 0%~(all equal)
EncodeJSON/FlowToken.TokensWithdrawn-2672B ± 0%672B ± 0%~(all equal)
ExportType/composite_type-2136B ± 0%136B ± 0%~(all equal)
ExportType/simple_type-20.00B 0.00B ~(all equal)
InterpretRecursionFib-21.00MB ± 0%1.00MB ± 0%~(all equal)
NewInterpreter/new_interpreter-2768B ± 0%768B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-2200B ± 0%200B ± 0%~(all equal)
ParseArray-22.80MB ± 0%2.80MB ± 0%~(p=1.000 n=1+1)
ParseDeploy/byte_array-24.09MB ± 0%4.09MB ± 0%~(p=1.000 n=1+1)
ParseDeploy/decode_hex-2214kB ± 0%214kB ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-228.9kB ± 0%28.9kB ± 0%~(p=1.000 n=1+1)
ParseFungibleToken/Without_memory_metering-228.9kB ± 0%28.9kB ± 0%~(p=1.000 n=1+1)
ParseInfix-21.92kB ± 0%1.91kB ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeResourceDictionaryValues-22.28MB ± 0%2.28MB ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-22.70kB ± 0%2.70kB ± 0%~(all equal)
SuperTypeInference/arrays-296.0B ± 0%96.0B ± 0%~(all equal)
SuperTypeInference/composites-20.00B 0.00B ~(all equal)
SuperTypeInference/integers-20.00B 0.00B ~(all equal)
ValueIsSubtypeOfSemaType-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-2806 ± 0%806 ± 0%~(all equal)
ContractInterfaceFungibleToken-2370 ± 0%370 ± 0%~(all equal)
DecodeBatchEventsCCF-21.48M ± 0%1.48M ± 0%~(p=1.000 n=1+1)
DecodeBatchEventsJSON-24.75M ± 0%4.75M ± 0%~(p=1.000 n=1+1)
DecodeCCF/FlowFees.FeesDeducted-230.0 ± 0%30.0 ± 0%~(all equal)
DecodeCCF/FlowFees.TokensWithdrawn-226.0 ± 0%26.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-230.0 ± 0%30.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-232.0 ± 0%32.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.NewWeeklyPayout-226.0 ± 0%26.0 ± 0%~(all equal)
DecodeCCF/FlowIDTableStaking.RewardsPaid-229.0 ± 0%29.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited-231.0 ± 0%31.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensDeposited_with_nil_receiver-229.0 ± 0%29.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensMinted-226.0 ± 0%26.0 ± 0%~(all equal)
DecodeCCF/FlowToken.TokensWithdrawn-231.0 ± 0%31.0 ± 0%~(all equal)
DecodeJSON/FlowFees.FeesDeducted-2129 ± 0%129 ± 0%~(all equal)
DecodeJSON/FlowFees.TokensWithdrawn-272.0 ± 0%72.0 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-2103 ± 0%103 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-2163 ± 0%163 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.NewWeeklyPayout-271.0 ± 0%71.0 ± 0%~(all equal)
DecodeJSON/FlowIDTableStaking.RewardsPaid-288.0 ± 0%88.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited-296.0 ± 0%96.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensDeposited_with_nil_receiver-287.0 ± 0%87.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensMinted-272.0 ± 0%72.0 ± 0%~(all equal)
DecodeJSON/FlowToken.TokensWithdrawn-296.0 ± 0%96.0 ± 0%~(all equal)
EncodeBatchEventsCCF-2564k ± 0%564k ± 0%~(p=1.000 n=1+1)
EncodeBatchEventsJSON-2757k ± 0%757k ± 0%~(p=1.000 n=1+1)
EncodeCCF/FlowFees.FeesDeducted-211.0 ± 0%11.0 ± 0%~(all equal)
EncodeCCF/FlowFees.TokensWithdrawn-29.00 ± 0%9.00 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.DelegatorRewardsPaid-211.0 ± 0%11.0 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.EpochTotalRewardsPaid-211.0 ± 0%11.0 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.NewWeeklyPayout-29.00 ± 0%9.00 ± 0%~(all equal)
EncodeCCF/FlowIDTableStaking.RewardsPaid-211.0 ± 0%11.0 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited-212.0 ± 0%12.0 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensDeposited_with_nil_receiver-212.0 ± 0%12.0 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensMinted-29.00 ± 0%9.00 ± 0%~(all equal)
EncodeCCF/FlowToken.TokensWithdrawn-212.0 ± 0%12.0 ± 0%~(all equal)
EncodeJSON/FlowFees.FeesDeducted-217.0 ± 0%17.0 ± 0%~(all equal)
EncodeJSON/FlowFees.TokensWithdrawn-210.0 ± 0%10.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.DelegatorRewardsPaid-214.0 ± 0%14.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.EpochTotalRewardsPaid-223.0 ± 0%23.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.NewWeeklyPayout-210.0 ± 0%10.0 ± 0%~(all equal)
EncodeJSON/FlowIDTableStaking.RewardsPaid-213.0 ± 0%13.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited-217.0 ± 0%17.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensDeposited_with_nil_receiver-212.0 ± 0%12.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensMinted-211.0 ± 0%11.0 ± 0%~(all equal)
EncodeJSON/FlowToken.TokensWithdrawn-216.0 ± 0%16.0 ± 0%~(all equal)
ExportType/composite_type-23.00 ± 0%3.00 ± 0%~(all equal)
ExportType/simple_type-20.00 0.00 ~(all equal)
InterpretRecursionFib-218.9k ± 0%18.9k ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-24.00 ± 0%4.00 ± 0%~(all equal)
ParseArray-259.6k ± 0%59.6k ± 0%~(p=1.000 n=1+1)
ParseDeploy/byte_array-289.4k ± 0%89.4k ± 0%~(p=1.000 n=1+1)
ParseDeploy/decode_hex-263.0 ± 0%63.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-2768 ± 0%768 ± 0%~(all equal)
ParseFungibleToken/Without_memory_metering-2768 ± 0%768 ± 0%~(all equal)
ParseInfix-248.0 ± 0%48.0 ± 0%~(all equal)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
RuntimeResourceDictionaryValues-236.9k ± 0%36.9k ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-243.0 ± 0%43.0 ± 0%~(all equal)
SuperTypeInference/arrays-23.00 ± 0%3.00 ± 0%~(all equal)
SuperTypeInference/composites-20.00 0.00 ~(all equal)
SuperTypeInference/integers-20.00 0.00 ~(all equal)
ValueIsSubtypeOfSemaType-21.00 ± 0%1.00 ± 0%~(all equal)
 

@codecov
Copy link

codecov bot commented Apr 23, 2023

Codecov Report

Merging #2463 (8e5eb0d) into master (d7f3648) will increase coverage by 0.02%.
The diff coverage is 92.80%.

@@            Coverage Diff             @@
##           master    #2463      +/-   ##
==========================================
+ Coverage   78.29%   78.32%   +0.02%     
==========================================
  Files         325      325              
  Lines       72465    72587     +122     
==========================================
+ Hits        56738    56851     +113     
- Misses      13644    13649       +5     
- Partials     2083     2087       +4     
Flag Coverage Δ
unittests 78.32% <92.80%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/parser/parser.go 90.57% <ø> (ø)
runtime/sema/gen/main.go 89.34% <ø> (ø)
runtime/parser/statement.go 80.62% <50.00%> (+0.43%) ⬆️
runtime/sema/checker.go 92.50% <89.47%> (-0.11%) ⬇️
runtime/sema/check_function.go 98.93% <96.15%> (-0.46%) ⬇️
runtime/parser/function.go 70.06% <100.00%> (-3.38%) ⬇️
runtime/sema/check_composite_declaration.go 95.75% <100.00%> (+0.01%) ⬆️
runtime/sema/check_interface_declaration.go 96.70% <100.00%> (+0.08%) ⬆️
runtime/sema/check_transaction_declaration.go 84.77% <100.00%> (+0.15%) ⬆️
runtime/sema/errors.go 93.12% <100.00%> (+0.04%) ⬆️

... and 2 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

if items.length < 1 {
return nil
}
return items[0]
Copy link
Contributor

@dsainati1 dsainati1 Apr 24, 2023

Choose a reason for hiding this comment

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

I think we should have some more tests for these; in particular I'd like to see tests for when a generically-typed value "escapes" the scope of its function into an external/global variable. E.g. if I have a function like this that instead stores items[0] in a global array of type [AnyStruct] vs one of type [String], and how these subtyping checks interact with the type parameter bound.

Similarly imo we would also want tests to make sure that the type parameter is usable as a value of its bound type inside the function body; e.g. if I have a T: [AnyStruct] i'd expect to be able to call array functions on a value of type T.

Copy link
Contributor

@dsainati1 dsainati1 left a comment

Choose a reason for hiding this comment

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

This is cool! Generics is usually a pretty complicated feature in how it interacts with the rest of a language's features though, so even if it's simple to enable here I think we want to make sure we test it thoroughly before enabling it.

I also think we do need to add some interpreter tests to this PR. Even though we don't add support here for using the type variable inside the body of the function (like in a cast), it's still possible to downcast a value that is typed with a variable. E.g. if you have some parameter v: T where T is a type variable, you might have a later expression v as? X where X is some other type. IMO it's worth adding tests cases to the interpreter for these sorts of uses. Most particularly we should test the cases where T is a reference type, even if it's declared with an AnyStruct type bound we want to make sure that the previously identified bug with casting between AnyStruct and reference types doesn't remanifest itself here.

@turbolent turbolent marked this pull request as draft April 24, 2023 15:09
@bluesign
Copy link
Contributor

bluesign commented Apr 25, 2023

This will be super useful to write clean code. Will this allow multiple Type parameters?
something like: pub fun cast<T, T2>(x: T): T2 (totally made up example, probably not makes sense here)

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

Successfully merging this pull request may close these issues.

None yet

3 participants