Class: RBS::Types::Function
- Inherits:
-
Object
- Object
- RBS::Types::Function
- Defined in:
- lib/rbs/types.rb
Defined Under Namespace
Classes: Param
Instance Attribute Summary collapse
-
#optional_keywords ⇒ Object
readonly
Returns the value of attribute optional_keywords.
-
#optional_positionals ⇒ Object
readonly
Returns the value of attribute optional_positionals.
-
#required_keywords ⇒ Object
readonly
Returns the value of attribute required_keywords.
-
#required_positionals ⇒ Object
readonly
Returns the value of attribute required_positionals.
-
#rest_keywords ⇒ Object
readonly
Returns the value of attribute rest_keywords.
-
#rest_positionals ⇒ Object
readonly
Returns the value of attribute rest_positionals.
-
#return_type ⇒ Object
readonly
Returns the value of attribute return_type.
-
#trailing_positionals ⇒ Object
readonly
Returns the value of attribute trailing_positionals.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #drop_head ⇒ Object
- #drop_tail ⇒ Object
- #each_param(&block) ⇒ Object
- #each_type ⇒ Object
- #empty? ⇒ Boolean
- #free_variables(set = Set.new) ⇒ Object
- #has_keyword? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(required_positionals:, optional_positionals:, rest_positionals:, trailing_positionals:, required_keywords:, optional_keywords:, rest_keywords:, return_type:) ⇒ Function
constructor
A new instance of Function.
- #map_type(&block) ⇒ Object
- #map_type_name(&block) ⇒ Object
- #param_to_s ⇒ Object
- #return_to_s ⇒ Object
- #sub(s) ⇒ Object
- #to_json(*a) ⇒ Object
- #update(required_positionals: self.required_positionals, optional_positionals: self.optional_positionals, rest_positionals: self.rest_positionals, trailing_positionals: self.trailing_positionals, required_keywords: self.required_keywords, optional_keywords: self.optional_keywords, rest_keywords: self.rest_keywords, return_type: self.return_type) ⇒ Object
- #with_return_type(type) ⇒ Object
Constructor Details
#initialize(required_positionals:, optional_positionals:, rest_positionals:, trailing_positionals:, required_keywords:, optional_keywords:, rest_keywords:, return_type:) ⇒ Function
Returns a new instance of Function.
709 710 711 712 713 714 715 716 717 718 |
# File 'lib/rbs/types.rb', line 709 def initialize(required_positionals:, optional_positionals:, rest_positionals:, trailing_positionals:, required_keywords:, optional_keywords:, rest_keywords:, return_type:) @return_type = return_type @required_positionals = required_positionals @optional_positionals = optional_positionals @rest_positionals = rest_positionals @trailing_positionals = trailing_positionals @required_keywords = required_keywords @optional_keywords = optional_keywords @rest_keywords = rest_keywords end |
Instance Attribute Details
#optional_keywords ⇒ Object (readonly)
Returns the value of attribute optional_keywords.
705 706 707 |
# File 'lib/rbs/types.rb', line 705 def optional_keywords @optional_keywords end |
#optional_positionals ⇒ Object (readonly)
Returns the value of attribute optional_positionals.
701 702 703 |
# File 'lib/rbs/types.rb', line 701 def optional_positionals @optional_positionals end |
#required_keywords ⇒ Object (readonly)
Returns the value of attribute required_keywords.
704 705 706 |
# File 'lib/rbs/types.rb', line 704 def required_keywords @required_keywords end |
#required_positionals ⇒ Object (readonly)
Returns the value of attribute required_positionals.
700 701 702 |
# File 'lib/rbs/types.rb', line 700 def required_positionals @required_positionals end |
#rest_keywords ⇒ Object (readonly)
Returns the value of attribute rest_keywords.
706 707 708 |
# File 'lib/rbs/types.rb', line 706 def rest_keywords @rest_keywords end |
#rest_positionals ⇒ Object (readonly)
Returns the value of attribute rest_positionals.
702 703 704 |
# File 'lib/rbs/types.rb', line 702 def rest_positionals @rest_positionals end |
#return_type ⇒ Object (readonly)
Returns the value of attribute return_type.
707 708 709 |
# File 'lib/rbs/types.rb', line 707 def return_type @return_type end |
#trailing_positionals ⇒ Object (readonly)
Returns the value of attribute trailing_positionals.
703 704 705 |
# File 'lib/rbs/types.rb', line 703 def trailing_positionals @trailing_positionals end |
Class Method Details
.empty(return_type) ⇒ Object
843 844 845 846 847 848 849 850 851 852 853 854 |
# File 'lib/rbs/types.rb', line 843 def self.empty(return_type) Function.new( required_positionals: [], optional_positionals: [], rest_positionals: nil, trailing_positionals: [], required_keywords: {}, optional_keywords: {}, rest_keywords: nil, return_type: return_type ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
720 721 722 723 724 725 726 727 728 729 730 |
# File 'lib/rbs/types.rb', line 720 def ==(other) other.is_a?(Function) && other.required_positionals == required_positionals && other.optional_positionals == optional_positionals && other.rest_positionals == rest_positionals && other.trailing_positionals == trailing_positionals && other.required_keywords == required_keywords && other.optional_keywords == optional_keywords && other.rest_keywords == rest_keywords && other.return_type == return_type end |
#drop_head ⇒ Object
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 |
# File 'lib/rbs/types.rb', line 910 def drop_head case when !required_positionals.empty? [ required_positionals[0], update(required_positionals: required_positionals.drop(1)) ] when !optional_positionals.empty? [ optional_positionals[0], update(optional_positionals: optional_positionals.drop(1)) ] else raise "Cannot #drop_head" end end |
#drop_tail ⇒ Object
927 928 929 930 931 932 933 934 935 936 937 |
# File 'lib/rbs/types.rb', line 927 def drop_tail case when !trailing_positionals.empty? [ trailing_positionals.last, update(trailing_positionals: trailing_positionals.take(trailing_positionals.size - 1)) ] else raise "Cannot #drop_tail" end end |
#each_param(&block) ⇒ Object
812 813 814 815 816 817 818 819 820 821 822 823 824 |
# File 'lib/rbs/types.rb', line 812 def each_param(&block) if block_given? required_positionals.each(&block) optional_positionals.each(&block) rest_positionals&.yield_self(&block) trailing_positionals.each(&block) required_keywords.each_value(&block) optional_keywords.each_value(&block) rest_keywords&.yield_self(&block) else enum_for :each_param end end |
#each_type ⇒ Object
797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'lib/rbs/types.rb', line 797 def each_type if block_given? required_positionals.each {|param| yield param.type } optional_positionals.each {|param| yield param.type } rest_positionals&.yield_self {|param| yield param.type } trailing_positionals.each {|param| yield param.type } required_keywords.each_value {|param| yield param.type } optional_keywords.each_value {|param| yield param.type } rest_keywords&.yield_self {|param| yield param.type } yield(return_type) else enum_for :each_type end end |
#empty? ⇒ Boolean
883 884 885 886 887 888 889 890 891 |
# File 'lib/rbs/types.rb', line 883 def empty? required_positionals.empty? && optional_positionals.empty? && !rest_positionals && trailing_positionals.empty? && required_keywords.empty? && optional_keywords.empty? && !rest_keywords end |
#free_variables(set = Set.new) ⇒ Object
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
# File 'lib/rbs/types.rb', line 746 def free_variables(set = Set.new) set.tap do required_positionals.each do |param| param.type.free_variables(set) end optional_positionals.each do |param| param.type.free_variables(set) end rest_positionals&.yield_self do |param| param.type.free_variables(set) end trailing_positionals.each do |param| param.type.free_variables(set) end required_keywords.each_value do |param| param.type.free_variables(set) end optional_keywords.each_value do |param| param.type.free_variables(set) end rest_keywords&.yield_self do |param| param.type.free_variables(set) end return_type.free_variables(set) end end |
#has_keyword? ⇒ Boolean
939 940 941 |
# File 'lib/rbs/types.rb', line 939 def has_keyword? !required_keywords.empty? || !optional_keywords.empty? || rest_keywords end |
#hash ⇒ Object
734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/rbs/types.rb', line 734 def hash self.class.hash ^ required_positionals.hash ^ optional_positionals.hash ^ rest_positionals.hash ^ trailing_positionals.hash ^ required_keywords.hash ^ optional_keywords.hash ^ rest_keywords.hash ^ return_type.hash end |
#map_type(&block) ⇒ Object
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 |
# File 'lib/rbs/types.rb', line 774 def map_type(&block) if block_given? Function.new( required_positionals: required_positionals.map {|param| param.map_type(&block) }, optional_positionals: optional_positionals.map {|param| param.map_type(&block) }, rest_positionals: rest_positionals&.yield_self {|param| param.map_type(&block) }, trailing_positionals: trailing_positionals.map {|param| param.map_type(&block) }, required_keywords: required_keywords.transform_values {|param| param.map_type(&block) }, optional_keywords: optional_keywords.transform_values {|param| param.map_type(&block) }, rest_keywords: rest_keywords&.yield_self {|param| param.map_type(&block) }, return_type: yield(return_type) ) else enum_for :map_type end end |
#map_type_name(&block) ⇒ Object
791 792 793 794 795 |
# File 'lib/rbs/types.rb', line 791 def map_type_name(&block) map_type do |type| type.map_type_name(&block) end end |
#param_to_s ⇒ Object
893 894 895 896 897 898 899 900 901 902 903 904 |
# File 'lib/rbs/types.rb', line 893 def param_to_s params = [] params.push(*required_positionals.map(&:to_s)) params.push(*optional_positionals.map {|p| "?#{p}"}) params.push("*#{rest_positionals}") if rest_positionals params.push(*trailing_positionals.map(&:to_s)) params.push(*required_keywords.map {|name, param| "#{name}: #{param}" }) params.push(*optional_keywords.map {|name, param| "?#{name}: #{param}" }) params.push("**#{rest_keywords}") if rest_keywords params.join(", ") end |
#return_to_s ⇒ Object
906 907 908 |
# File 'lib/rbs/types.rb', line 906 def return_to_s return_type.to_s(1) end |
#sub(s) ⇒ Object
839 840 841 |
# File 'lib/rbs/types.rb', line 839 def sub(s) map_type {|ty| ty.sub(s) } end |
#to_json(*a) ⇒ Object
826 827 828 829 830 831 832 833 834 835 836 837 |
# File 'lib/rbs/types.rb', line 826 def to_json(*a) { required_positionals: required_positionals, optional_positionals: optional_positionals, rest_positionals: rest_positionals, trailing_positionals: trailing_positionals, required_keywords: required_keywords, optional_keywords: optional_keywords, rest_keywords: rest_keywords, return_type: return_type }.to_json(*a) end |
#update(required_positionals: self.required_positionals, optional_positionals: self.optional_positionals, rest_positionals: self.rest_positionals, trailing_positionals: self.trailing_positionals, required_keywords: self.required_keywords, optional_keywords: self.optional_keywords, rest_keywords: self.rest_keywords, return_type: self.return_type) ⇒ Object
869 870 871 872 873 874 875 876 877 878 879 880 881 |
# File 'lib/rbs/types.rb', line 869 def update(required_positionals: self.required_positionals, optional_positionals: self.optional_positionals, rest_positionals: self.rest_positionals, trailing_positionals: self.trailing_positionals, required_keywords: self.required_keywords, optional_keywords: self.optional_keywords, rest_keywords: self.rest_keywords, return_type: self.return_type) Function.new( required_positionals: required_positionals, optional_positionals: optional_positionals, rest_positionals: rest_positionals, trailing_positionals: trailing_positionals, required_keywords: required_keywords, optional_keywords: optional_keywords, rest_keywords: rest_keywords, return_type: return_type ) end |
#with_return_type(type) ⇒ Object
856 857 858 859 860 861 862 863 864 865 866 867 |
# File 'lib/rbs/types.rb', line 856 def with_return_type(type) Function.new( required_positionals: required_positionals, optional_positionals: optional_positionals, rest_positionals: rest_positionals, trailing_positionals: trailing_positionals, required_keywords: required_keywords, optional_keywords: optional_keywords, rest_keywords: rest_keywords, return_type: type ) end |