Set both default attribute and type of attribute

Graham Slick

I want to define a class Person with a age attribute of type Int32 and specify a default value for it in case it's not provided. I know how to do the first one:

class Person

  def initialize(@age : Int32) 

  end

end

and the second one:

class Person

  def initialize(@age = 0) 

  end

end

But not how to do both. Is this possible ?

Oleh Prypin

The ability to do this was added relatively recently, and it seems to be missing in the documentation. This is the way to do it:

class Person
  def initialize(@age : Int32 = 0) 
  end
end

Note that by default the type is implied to be the same as that of the default value. For example:

class Person
  def initialize(@age = 0) 
  end
end

Person.new("a")
Error in line 6: instantiating 'Person:Class#new(String)'
in line 2: instance variable '@age' of Person must be Int32, not String

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Default arguments in Function attribute of type

From Dev

Get all attributes of an attribute set that is not present in default attribute set

From Dev

Custom styled attribute always set by default

From Dev

Is there a way to set a default value for a chef resource attribute?

From Dev

How to get default product attribute set id?

From Dev

How to set the default value of directive attribute?

From Dev

Set default attribute to a Polymer content tag

From Dev

Custom styled attribute always set by default

From Dev

Get default value if attribute not set or faulty in Javascript

From Dev

Default Content Type Attribute value for Page Directive

From Dev

What is the default type for an attribute in an xml schema?

From Dev

Is there a way to set attribute of attribute?

From Dev

Is there a way to set attribute of attribute?

From Dev

Nix value is not of type `attribute set of signed integers'

From Dev

XML Schema set a value of attribute of a specific type

From Dev

Yii2: How to set default attribute values in ActiveRecord?

From Dev

How to set a default attribute value for a Laravel / Eloquent model?

From Dev

How to set a default value for attribute if it doesn't exist in the XDocument object

From Dev

Java: how to set default value to annotation with another annotation as its attribute?

From Dev

Factory Girl: Set default value of transient attribute to factory

From Dev

VertexArrayObject, how can i set "default" attribute value?

From Dev

Ways to set default value for class attribute without using hasattr

From Dev

Magmi Multi-Store set attribute to "Use Default Value"

From Dev

How to set a default value for attribute if it doesn't exist in the XDocument object

From Dev

JQ Datepicker - Set Default date from Data Id Attribute

From Dev

XML Schema: what's the default type of an xsd:attribute?

From Dev

In Struts 1, What is the Default Value of Type Attribute of <action> tag?

From Dev

Default value of the entity attribute

From Dev

Using Both of Attribute and Convention Routing

Related Related

  1. 1

    Default arguments in Function attribute of type

  2. 2

    Get all attributes of an attribute set that is not present in default attribute set

  3. 3

    Custom styled attribute always set by default

  4. 4

    Is there a way to set a default value for a chef resource attribute?

  5. 5

    How to get default product attribute set id?

  6. 6

    How to set the default value of directive attribute?

  7. 7

    Set default attribute to a Polymer content tag

  8. 8

    Custom styled attribute always set by default

  9. 9

    Get default value if attribute not set or faulty in Javascript

  10. 10

    Default Content Type Attribute value for Page Directive

  11. 11

    What is the default type for an attribute in an xml schema?

  12. 12

    Is there a way to set attribute of attribute?

  13. 13

    Is there a way to set attribute of attribute?

  14. 14

    Nix value is not of type `attribute set of signed integers'

  15. 15

    XML Schema set a value of attribute of a specific type

  16. 16

    Yii2: How to set default attribute values in ActiveRecord?

  17. 17

    How to set a default attribute value for a Laravel / Eloquent model?

  18. 18

    How to set a default value for attribute if it doesn't exist in the XDocument object

  19. 19

    Java: how to set default value to annotation with another annotation as its attribute?

  20. 20

    Factory Girl: Set default value of transient attribute to factory

  21. 21

    VertexArrayObject, how can i set "default" attribute value?

  22. 22

    Ways to set default value for class attribute without using hasattr

  23. 23

    Magmi Multi-Store set attribute to "Use Default Value"

  24. 24

    How to set a default value for attribute if it doesn't exist in the XDocument object

  25. 25

    JQ Datepicker - Set Default date from Data Id Attribute

  26. 26

    XML Schema: what's the default type of an xsd:attribute?

  27. 27

    In Struts 1, What is the Default Value of Type Attribute of <action> tag?

  28. 28

    Default value of the entity attribute

  29. 29

    Using Both of Attribute and Convention Routing

HotTag

Archive