How to get fields of a Julia object

For v0.7+ Use fieldnames(x), where x is a DataType. For example, use fieldnames(Date), instead of fieldnames(today()), or else use fieldnames(typeof(today())). This returns Vector{Symbol} listing the field names in order. If a field name is myfield, then to retrieve the values in that field use either getfield(x, :myfield), or the shortcut syntax x.myfield. Another useful and … Read more

C#, immutability and public readonly fields

C# 6.0 now supports auto-property initializers. The auto-property initializer allows assignment of properties directly within their declaration. For read-only properties, it takes care of all the ceremony required to ensure the property is immutable. You can initialize read-only properties in constructor or using auto-initializer public class Customer { public Customer3(string firstName, string lastName) { FirstName … Read more

Why is a `val` inside an `object` not automatically final?

This is addressed explicitly in the specification, and they are automatically final: Members of final classes or objects are implicitly also final, so the final modifier is generally redundant for them, too. Note, however, that constant value definitions (ยง4.1) do require an explicit final modifier, even if they are defined in a final class or … Read more

Loop over all fields in a Java class

Use getDeclaredFields on [Class] ClasWithStuff myStuff = new ClassWithStuff(); Field[] fields = myStuff.getClass().getDeclaredFields(); for(Field f : fields){ Class t = f.getType(); Object v = f.get(myStuff); if(t == boolean.class && Boolean.FALSE.equals(v)) // found default value else if(t.isPrimitive() && ((Number) v).doubleValue() == 0) // found default value else if(!t.isPrimitive() && v == null) // found default value … Read more

Django: Admin: changing the widget of the field in Admin

UPDATE 1: Code that gets me done with 1) (don’t forget tot pass CHOICES to the BooleanField in the model) from main.models import TagCat from django.contrib import admin from django import forms class MyTagCatAdminForm(forms.ModelForm): class Meta: model = TagCat widgets = { ‘by_admin’: forms.RadioSelect } fields=”__all__” # required for Django 3.x class TagCatAdmin(admin.ModelAdmin): form = … Read more

Number of fields returned by awk

The NF variable is set to the total number of fields in the input record. So: echo “a b c d” | awk –field-separator=” ” “{ print NF }” will display 4 Note, however, that: echo -e “a b c d\na b” | awk –field-separator=” ” “{ print NF }” will display: 4 2 Hope … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)